Liebe Comunity,
ich habe heute von meinem Lehrer ein Beispiel Java Code bekommen und kann damit absoulut nichts anfangen.
Könntet ihr mir diesen Jcode etwas erläutern was welche Zeilen oder Methoden etc. machen?
Das wäre toll. Schon mal Danke im Voraus!
Hier der Code:
dann noch:
DANKE!!
ich habe heute von meinem Lehrer ein Beispiel Java Code bekommen und kann damit absoulut nichts anfangen.
Könntet ihr mir diesen Jcode etwas erläutern was welche Zeilen oder Methoden etc. machen?
Das wäre toll. Schon mal Danke im Voraus!
Hier der Code:
Java:
package kondensator;
import java.io.File;
import java.util.Scanner;
public class Menue {
private Scanner s;
public Menue() {
s = new Scanner(System.in);
}
public void datenEingabe() {
System.out.println("Um eine Kondensator Aufladung zu simulieren, werden einige Werte benötigt.");
boolean b = true;
double spannung = 0;
while(b) {
System.out.println("Geben Sie bitte zunächst einen Wert für die Spannung (in Volt) ein!");
String in = s.nextLine();
try {
spannung = Double.valueOf(in.replaceAll(",", "."));
b = false;
} catch (NumberFormatException e) {
System.out.println("Ungültige Eingabe: " + in);
b = true;
}
}
b = true;
double stromstaerke = 0;
while(b) {
System.out.println("Geben Sie nun bitte einen Wert für die Stromstärke (in Ampere) ein!");
String in = s.nextLine();
try {
stromstaerke = Double.valueOf(in.replaceAll(",", "."));
b = false;
} catch (NumberFormatException e) {
System.out.println("Ungültige Eingabe: " + in);
b = true;
}
}
b = true;
double kapazitaet = 0;
while(b) {
System.out.println("Geben Sie jetzt einen Wert für die Kapazität (in Farad) ein!");
String in = s.nextLine();
try {
kapazitaet = Double.valueOf(in.replaceAll(",", "."));
b = false;
} catch (NumberFormatException e) {
System.out.println("Ungültige Eingabe: " + in);
b = true;
}
}
b = true;
double vorwiderstand = 0;
while(b) {
System.out.println("Geben Sie jetzt den Wert für den Vorwiderstand (in Ohm) ein!");
String in = s.nextLine();
try {
vorwiderstand = Double.valueOf(in.replaceAll(",", "."));
b = false;
} catch (NumberFormatException e) {
System.out.println("Ungültige Eingabe: " + in);
b = true;
}
}
b = true;
double tMax = 0;
while(b) {
System.out.println("Jetzt wird nur noch der Zeitpunkt (in Sekunden) benötigt, bis zu dem die Spannung und die Stromstärke ausgerechnet werden sollen.");
String in = s.nextLine();
try {
tMax = Double.valueOf(in.replaceAll(",", "."));
b = false;
} catch (NumberFormatException e) {
System.out.println("Ungültige Eingabe: " + in);
b = true;
}
}
KondensatorAufladung k = new KondensatorAufladung(spannung, stromstaerke, kapazitaet, vorwiderstand, tMax);
System.out.println("Die Aufladung wurde simuliert.");
b = true;
while(b) {
System.out.println("Sollen die Ergebnisse ausgegeben werden?");
System.out.println("0 - ja");
System.out.println("1 - nein");
String in = s.nextLine();
switch (in) {
case "0":
System.out.println("Ergebnisse der Simulation:");
k.gibErgebnisseAus();
b = false;
break;
case "1":
b = false;
break;
default:
System.out.println("Ungültige Eingabe: " + in);
break;
}
}
b = true;
boolean saveCsvFile = false;
while(b) {
System.out.println("Sollen die Ergebnisse in einer CSV Datei gespeichert werden?");
System.out.println("0 - ja");
System.out.println("1 - nein");
String in = s.nextLine();
switch (in) {
case "0":
saveCsvFile = true;
b = false;
break;
case "1":
saveCsvFile = false;
b = false;
break;
default:
System.out.println("Ungültige Eingabe: " + in);
break;
}
}
if(saveCsvFile) {
String dateipfad = System.getProperty("user.home");
if(System.getProperty("os.name").toLowerCase().startsWith("mac")) {
dateipfad = dateipfad + "/Desktop";
} else {
dateipfad = dateipfad + "\\Desktop";
}
b = true;
while(b) {
System.out.println("Wo soll die Datei gespeichert werden? Geben Sie entweder einen Pfad ein oder drücken Sie Enter, wenn die Datei auf dem Desktop gespeichert werden soll.");
String in = s.nextLine();
if(in.equals("")) {
System.out.println("Die Datei wird auf dem Desktop gespeichert werden.");
b = false;
} else {
File f = new File(in);
if(f.exists()&&f.isDirectory()) {
b = false;
dateipfad = in;
System.out.println("Die Datei wird im Verzeichnis " + in + " gespeichert werden.");
} else {
System.out.println("'" + in + "' in ist kein Verzeichnis!");
}
}
}
String dateiname = "";
b = true;
while(b) {
System.out.println("Geben Sie nun bitte einen Namen für die CSV Datei ein!");
String in = s.nextLine();
if(!in.equals("")) {
b = false;
dateiname = in;
} else {
System.out.println("Keine Eingabe ist hier nicht möglich!");
}
}
k.speichereErgebnisseInCSVDatei(dateiname, dateipfad);
System.out.println("'" + dateiname + ".csv' wurde unter " + dateipfad + " gespeichert!");
}
b = true;
while(b) {
System.out.println("Möchten Sie eine weitere Simulation durchführen oder das Programm beenden?");
System.out.println("0 - weitere Simulation");
System.out.println("1 - Programm beenden");
String in = s.nextLine();
switch (in) {
case "0":
datenEingabe();
b = false;
break;
case "1":
System.out.println("Programm beenden...");
System.exit(0);
b = false;
break;
default:
System.out.println("Ungültige Eingabe: " + in);
break;
}
}
}
}
dann noch:
Java:
package kondensator;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class KondensatorAufladung {
private double spannung;
private double stromstaerke;
private double vorwiderstand;
private double kapazitaet;
private double tMax;
private double tIntervall;
private double T;
private double[] UVont;
private double[] IVont;
private DecimalFormat decimalFormat;
/**Simuliert eine Kondensator Aufladung und speichert 100 Werte für U(t) und I(t).
*
* @param spannung die Spannung von U0 in Volt
* @param stromstaerke die Stromstärke von I0 in Ampere
* @param kapazitaet die Kapazität in Farad
* @param vorwiderstand der Vorwiderstand in Ohm
* @param tMax der Maximalwert der Zeitskala in Sekunden
*/
public KondensatorAufladung(double spannung, double stromstaerke, double kapazitaet, double vorwiderstand, double tMax) {
this.spannung = spannung;
this.stromstaerke = stromstaerke;
this.kapazitaet = kapazitaet;
this.vorwiderstand = vorwiderstand;
this.tMax = tMax;
this.decimalFormat = new DecimalFormat("#.####");
this.tIntervall = tMax/100;
this.T = vorwiderstand*kapazitaet;
this.UVont = new double[100];
this.IVont = new double[100];
double t;
for(int i = 0; i<100; i++) {
t = tIntervall*i;
UVont[i] = spannung * (1-Math.pow(Math.E, (-(1/T)*t)));
IVont[i] = stromstaerke * Math.pow(Math.E, (-(1/T)*t));
}
}
public void gibErgebnisseAus() {
System.out.println();
System.out.println("U = " + spannung + " V");
System.out.println("I = " + stromstaerke + " A");
System.out.println("R = " + vorwiderstand + " Ω");
System.out.println("C = " + kapazitaet + " F");
System.out.println("tMax = " + tMax + " s");
System.out.println("tIntervall = " + tIntervall + " s");
System.out.println("T = " + T);
System.out.println();
System.out.println("| t | U(t) | I(t) |");
System.out.println("______________________________________");
DecimalFormat timeFormat = new DecimalFormat("#.##");
double t;
for(int i = 0; i<100; i++) {
t = tIntervall*i;
System.out.println("| " + stringAuffuellen(timeFormat.format(t)+" s", 10) + "| " + stringAuffuellen(decimalFormat.format(UVont[i]) + " V", 10) + "| " + stringAuffuellen(decimalFormat.format(IVont[i]) + " A",10) + " |");
}
System.out.println();
}
public void speichereErgebnisseInCSVDatei(String dateiname, String dateipfad) {
String[] array = new String[110];
NumberFormat numberformat = NumberFormat.getNumberInstance(Locale.GERMANY);
array[0] = "U (in V);" + numberformat.format(spannung);
array[1] = "I (in A);" + numberformat.format(stromstaerke);
array[2] = "R (in Ohm);" + numberformat.format(vorwiderstand);
array[3] = "C (in F);" + numberformat.format(kapazitaet);
array[4] = "tMax (in s);" + numberformat.format(tMax);
array[5] = "tIntervall (in s);=B5/100";
array[6] = "T;=B3*B4";
array[7] = "";
array[8] = "t;U(t);I(t)";
int z = 9;
for(int i = 0; i<101; i++) {
array[z] = "="+i+"*B6;=B1*(1-EXP(-(1/B7)*A"+(z+1)+"));=B2*EXP(-(1/B7)*A"+(z+1)+")";
z++;
}
if(array.length<1) {
return;
}
File path = new File(dateipfad);
if(!path.exists()) {
path.mkdirs();
}
File file = new File(path + "\\" +dateiname + ".csv");
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
System.err.println("Datei " + dateiname + ".csv konnte nicht erstellt werden!");
e.printStackTrace();
return;
}
}
try {
FileWriter filewriter = new FileWriter(file);
BufferedWriter bufferedwriter = new BufferedWriter(filewriter);
for(String line : array) {
bufferedwriter.write(line);
bufferedwriter.newLine();
}
bufferedwriter.close();
filewriter.close();
} catch (IOException e) {
System.err.println("Werte konnten nicht in '" + dateiname + ".csv geschrieben werden!");
e.printStackTrace();
return;
}
}
private String stringAuffuellen(String s, int bis) {
while(s.length()<bis) {
s = s + " ";
}
return s;
}
}
DANKE!!
Zuletzt bearbeitet von einem Moderator: