![]() |
|
|||||||
| Java Basics - Anfänger-Themen Fragen ausschließlich zu Java-Grundlagen von Ein- und Umsteigern |
|
|
|
Themen-Optionen | Thema durchsuchen | Ansicht |
| #1 (permalink) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Neuer Benutzer
short
Registriert seit: 09.04.2008
Fachbeiträge: 16
Abgegebene Danke: 0
Erhielt 0 Danke für 0 Beiträge
|
Hiho,
wir haben in der Schule mit Methoden und Zugriffe auf andere Klassen begonnen und haben dafür eine Hausaufgabe auf. Naja es ist nicht direkt eine Hausaufgabe, aber wir können uns schonmal in das Thema reinarbeiten und das möchte ich gerne machen, allerdings habe ich leichte Verständnisprobleme. Ich hoffe ihr könnt mir dabei behilflich sein. Die Aufgabe besteht aus 6 Teilen. Die Basis habe ich erstellt, weiß allerdings nicht ob diese richtig ist. Hier einmal die Aufgabenstellung:
Code:
public class SomeMaths
{
double pi = 3.14159;
double square;
public double getSquare(double a)
{
square = a * a;
return square;
}
}
Ich wäre euch sehr dankbar, wenn ihr mir dabei helfen könntet, da wir die Aufgabe morgen besprechen. Ich hätte die Aufgabe halt gerne gelöst bis morgen, damit ich das Verständnis dafür schon habe. Am besten wären schon die Lösungsansätze als Code, zumindest so, dass ich den Zusammenhang nachvollziehen kann. Ich bedanke mich schonmal. Gruß Andreas |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #2 (permalink) | |
|
Stammbenutzer
Team RPGenesis
Viertel Megabyte Registriert seit: 20.03.2008
Fachbeiträge: 409
Abgegebene Danke: 1
Erhielt 5 Danke für 5 Beiträge
|
Code:
public class SomeMaths
{
// Konstanten großschreiben
public static final double PI = 3.14159; // bzw double PI = Math.PI
public static double getSquare( double d ) // hab vorher das static vergessen :oops:
{
return d*d;
}
}
Zu Frage 2: wenn die Klassen SomeMaths und Circle in der gleichen Package( Ordner ) liegen ( ansonsten per import ), kannst du in Circle einfach folgendes Schreiben: Code:
double pi = SomeMaths.PI; CircleExercise muss dann auch in der gleichen Package liegen, und eine main-Methode definieren, in der du dann wie folgt ein neues Circle-Objekt erstellen kannst: Code:
Circle c1 = new Circle( 3 ); // mit dem dafür implementierten Konstruktor in Circle, 3 entspricht dem Radius Gruß andre111 |
|
|
| #3 (permalink) | |
|
Nicht angemeldet
Fachbeiträge: n/a
|
Die 1 ist eigentlich richtig, sinvoller wäre es aber so:
Code:
public class SomeMaths
{
final static double pi = 3.14159; // Konstanten sollten immer final static sein, warum kannst du in vielen Büchern nachlesen
public static double getSquare(double a)
{
return a * a; // du brauchst hierfür keine extra Variable.
}
}
2. Ja. auf den inhalt von SomeMaths kannst du mit SomeMaths.pi bzw. SomeMaths.getSquare(x); zugreifen, da diese jetzt static sind. 3-5 Jep, du machst ne 3. Klasse, wo du in der main 3 Circle-Objecte mit unterschiedlichen Werten erstellst 6. Mach doch in der Klasse Circle die void-Methode print, wo mit System.out.println das entsprechende von diesem Kreis ausgegeben wird |
| #5 (permalink) | |
|
Stammbenutzer
Team RPGenesis
Viertel Megabyte Registriert seit: 20.03.2008
Fachbeiträge: 409
Abgegebene Danke: 1
Erhielt 5 Danke für 5 Beiträge
|
oder er kann auch einfach die Methode
String toString() aus Object überschreiben( @AndreasBaum einfach die Methode definieren die einen String zurückgibt ), da diese automatisch in System.out.println() / print() aufgerufen wird. @AndreasBaum z.B. Code:
public String toString()
{
return "Kreis(Radius="+radius+")";
}
|
|
|
| #6 (permalink) | |
|
Neuer Benutzer
short
Themenstarter
Registriert seit: 09.04.2008
Fachbeiträge: 16
Abgegebene Danke: 0
Erhielt 0 Danke für 0 Beiträge
|
Wow, das ist jetzt ziemlich viel auf einmal.
Die SomeMaths Klasse habe ich jetzt so: Code:
public class SomeMaths
{
public static final double PI = 3.14159;
public static double getSquare( double d )
{
return d*d;
}
}
Ich habe jetzt nur folgendes: Code:
public class Circle
{
}
Code:
public class CircleExercise
{
}
Könnt ihr mir den Aufbau demonstrieren? |
|
|
| #7 (permalink) | |
|
Stammbenutzer
Team RPGenesis
Viertel Megabyte Registriert seit: 20.03.2008
Fachbeiträge: 409
Abgegebene Danke: 1
Erhielt 5 Danke für 5 Beiträge
|
Code:
public class Circle
{
private double radius;
public Circle( double rad ) // Konstruktor
{
radius = rad;
}
public double getCircumference()
{
// Versuchs selber zu definieren, nach Vorgabe
// hast du bei SomeMaths ja auch schon
}
public double getArea()
{
// ...
}
public double getRadius()
{
/ ...
}
public String toString()
{
// ...
}
}
Code:
public class CircleExercise
{
public static void main( String[] args )
{
Circle c = new Circle( 0.5 ); // Als Beispiel
}
}
|
|
|
| #8 (permalink) | |
|
Stammbenutzer
Megabyte
Registriert seit: 01.05.2008
Fachbeiträge: 2.009
Abgegebene Danke: 8
Erhielt 123 Danke für 122 Beiträge
|
was in der Klasse Circle drin sein soll, steht doch in der Aufgabenstellung .. die 1. Klasse hast doch ganz gut alleine geschafft, da sollte Circle nicht das Problem sein.
in der CircelExercise muss folgende methode drin sein Code:
public static void main(String[] args)
__________________
Wer aufhört, besser zu werden, hat aufgehört, gut zu sein. (Philip Rosenthal) |
|
|
| #9 (permalink) | |
|
Neuer Benutzer
short
Themenstarter
Registriert seit: 09.04.2008
Fachbeiträge: 16
Abgegebene Danke: 0
Erhielt 0 Danke für 0 Beiträge
|
Code:
public class CircleExercise
{
public static void main( String[] args )
{
Circle c1 = new Circle( 3.0 );
Circle c2 = new Circle( 8.0 );
Circle c3 = new Circle( 5.0 );
}
}
Ist das korrekt? Und zu der Circle.java Code:
public class Circle
{
private double radius;
private double circumference;
public Circle( double rad )
{
radius = rad;
}
public double getCircumference( double circum )
{
circumference = circum;
}
public double getArea()
{
}
public double getRadius()
{
}
public String toString()
{
}
}
|
|
|
| #10 (permalink) | |
|
Stammbenutzer
Team RPGenesis
Viertel Megabyte Registriert seit: 20.03.2008
Fachbeiträge: 409
Abgegebene Danke: 1
Erhielt 5 Danke für 5 Beiträge
|
Code:
public class Circle
{
// Die von dir definierten Variablen definierst du besser in den methoden selber
public Circle( double rad )
{
radius = rad;
}
public double getCircumference()
{
// Hier mit return die berechnung des umfangs zurückgeben ( mithilfe des radius und SomeMaths.PI )
// Wie bei SomeMaths in getSquare
}
public double getArea()
{
// Alle Methoden mit get gleich wie bei getCircumference
}
public double getRadius()
{
return radius; // Diese Funktion muss nichts berechnen und gibt nur den radius zurück;
}
public void setRadius( double rad )
{
radius = rad; // Setzt radius auf den übergebenen Wert rad
}
public String toString()
{
return "Kreis(Radius="+radius+")"; // Gibt einen String zurück der den Kreis beschreibt
}
}
CircleExercise ist ok |
|
|
| #11 (permalink) | |
|
Neuer Benutzer
short
Themenstarter
Registriert seit: 09.04.2008
Fachbeiträge: 16
Abgegebene Danke: 0
Erhielt 0 Danke für 0 Beiträge
|
Ich hoffe jetzt stimmt das:
Code:
public class Circle
{
public Circle( double rad )
{
radius = rad;
}
public double getCircumference()
{
return 2 * SomeMaths.PI * radius;
}
public double getArea()
{
return SomeMaths.PI * radius * radius;
}
public double getRadius()
{
return radius;
}
public void setRadius( double rad )
{
radius = rad;
}
public String toString()
{
return "Kreis(Radius="+radius+")";
}
}
|
|
|
| #12 (permalink) | |
|
Stammbenutzer
Team RPGenesis
Viertel Megabyte Registriert seit: 20.03.2008
Fachbeiträge: 409
Abgegebene Danke: 1
Erhielt 5 Danke für 5 Beiträge
|
Ja so stimmt es
Jetzt solltest du noch die restlichen aufgaben in CircleExcersice lösen // EDIT hab nicht gesehen dass die deklaration von double radius in der klasse fehlt |
|
|
| #13 (permalink) | |
|
Neuer Benutzer
short
Themenstarter
Registriert seit: 09.04.2008
Fachbeiträge: 16
Abgegebene Danke: 0
Erhielt 0 Danke für 0 Beiträge
|
Code:
public class SomeMaths
{
public static final double PI = 3.14159;
public static double getSquare( double d )
{
return d*d;
}
}
Code:
public class Circle
{
public Circle( double rad )
{
radius = rad;
}
public double getCircumference()
{
return 2 * SomeMaths.PI * radius;
}
public double getArea()
{
return SomeMaths.PI * radius * radius;
}
public double getRadius()
{
return radius;
}
public void setRadius( double rad )
{
radius = rad;
}
public String toString()
{
return "Kreis(Radius="+radius+")";
}
}
Code:
public class CircleExercise
{
public static void main( String[] args )
{
Circle c1 = new Circle( 3.0 );
Circle c2 = new Circle( 8.0 );
Circle c3 = new Circle( 5.0 );
}
}
So? Code:
public class CircleExercise
{
public static void main( String[] args )
{
Circle c1 = new Circle( 3.0 );
System.out.println("1. Kreis");
System.out.println("Radius: " +Circle.getRadius());
System.out.println("Umfang: " +Circle.getCircumference());
System.out.println("Flaeche: " +Circle.getArea());
Circle c2 = new Circle( 8.0 );
System.out.println("2. Kreis");
System.out.println("Radius: " +Circle.getRadius());
System.out.println("Umfang: " +Circle.getCircumference());
System.out.println("Flaeche: " +Circle.getArea());
Circle c3 = new Circle( 5.0 );
System.out.println("3. Kreis");
System.out.println("Radius: " +Circle.getRadius());
System.out.println("Umfang: " +Circle.getCircumference());
System.out.println("Flaeche: " +Circle.getArea());
}
}
Code:
Compiliere E:\Java\Kapitel 5 - Klassen, Attribute, Methoden\Circle.java mit Java-Compiler
Circle.java:5:9: cannot find symbol
symbol : variable radius
location: class Circle
radius = rad;
^
Circle.java:10:35: cannot find symbol
symbol : variable radius
location: class Circle
return 2 * SomeMaths.PI * radius;
^
Circle.java:15:31: cannot find symbol
symbol : variable radius
location: class Circle
return SomeMaths.PI * radius * radius;
^
Circle.java:15:40: cannot find symbol
symbol : variable radius
location: class Circle
return SomeMaths.PI * radius * radius;
^
Circle.java:20:16: cannot find symbol
symbol : variable radius
location: class Circle
return radius;
^
Circle.java:25:9: cannot find symbol
symbol : variable radius
location: class Circle
radius = rad;
^
Circle.java:30:32: cannot find symbol
symbol : variable radius
location: class Circle
return "Kreis(Radius="+radius+")";
^
7 errors
Compiliere E:\Java\Kapitel 5 - Klassen, Attribute, Methoden\SomeMaths.java mit Java-Compiler
E:\Java\Kapitel 5 - Klassen, Attribute, Methoden\SomeMaths.java erfolgreich compiliert!
Compiliere E:\Java\Kapitel 5 - Klassen, Attribute, Methoden\CircleExercise.java mit Java-Compiler
CircleExercise.java:8:46: non-static method getRadius() cannot be referenced from a static context
System.out.println("Radius: " +Circle.getRadius());
^
CircleExercise.java:9:46: non-static method getCircumference() cannot be referenced from a static context
System.out.println("Umfang: " +Circle.getCircumference());
^
CircleExercise.java:10:47: non-static method getArea() cannot be referenced from a static context
System.out.println("Flaeche: " +Circle.getArea());
^
CircleExercise.java:15:46: non-static method getRadius() cannot be referenced from a static context
System.out.println("Radius: " +Circle.getRadius());
^
CircleExercise.java:16:46: non-static method getCircumference() cannot be referenced from a static context
System.out.println("Umfang: " +Circle.getCircumference());
^
CircleExercise.java:17:47: non-static method getArea() cannot be referenced from a static context
System.out.println("Flaeche: " +Circle.getArea());
^
CircleExercise.java:22:46: non-static method getRadius() cannot be referenced from a static context
System.out.println("Radius: " +Circle.getRadius());
^
CircleExercise.java:23:46: non-static method getCircumference() cannot be referenced from a static context
System.out.println("Umfang: " +Circle.getCircumference());
^
CircleExercise.java:24:47: non-static method getArea() cannot be referenced from a static context
System.out.println("Flaeche: " +Circle.getArea());
^
.\Circle.java:5:9: cannot find symbol
symbol : variable radius
location: class Circle
radius = rad;
^
.\Circle.java:10:35: cannot find symbol
symbol : variable radius
location: class Circle
return 2 * SomeMaths.PI * radius;
^
.\Circle.java:15:31: cannot find symbol
symbol : variable radius
location: class Circle
return SomeMaths.PI * radius * radius;
^
.\Circle.java:15:40: cannot find symbol
symbol : variable radius
location: class Circle
return SomeMaths.PI * radius * radius;
^
.\Circle.java:20:16: cannot find symbol
symbol : variable radius
location: class Circle
return radius;
^
.\Circle.java:25:9: cannot find symbol
symbol : variable radius
location: class Circle
radius = rad;
^
.\Circle.java:30:32: cannot find symbol
symbol : variable radius
location: class Circle
return "Kreis(Radius="+radius+")";
^
16 errors
Compiliere E:\Java\Kapitel 5 - Klassen, Attribute, Methoden\Circle.java mit Java-Compiler
Circle.java:5:9: cannot find symbol
symbol : variable radius
location: class Circle
radius = rad;
^
Circle.java:10:35: cannot find symbol
symbol : variable radius
location: class Circle
return 2 * SomeMaths.PI * radius;
^
Circle.java:15:31: cannot find symbol
symbol : variable radius
location: class Circle
return SomeMaths.PI * radius * radius;
^
Circle.java:15:40: cannot find symbol
symbol : variable radius
location: class Circle
return SomeMaths.PI * radius * radius;
^
Circle.java:20:16: cannot find symbol
symbol : variable radius
location: class Circle
return radius;
^
Circle.java:25:9: cannot find symbol
symbol : variable radius
location: class Circle
radius = rad;
^
Circle.java:30:32: cannot find symbol
symbol : variable radius
location: class Circle
return "Kreis(Radius="+radius+")";
^
7 errors
Compiliere E:\Java\Kapitel 5 - Klassen, Attribute, Methoden\SomeMaths.java mit Java-Compiler
E:\Java\Kapitel 5 - Klassen, Attribute, Methoden\SomeMaths.java erfolgreich compiliert!
Compiliere E:\Java\Kapitel 5 - Klassen, Attribute, Methoden\CircleExercise.java mit Java-Compiler
CircleExercise.java:8:46: non-static method getRadius() cannot be referenced from a static context
System.out.println("Radius: " +Circle.getRadius());
^
CircleExercise.java:9:46: non-static method getCircumference() cannot be referenced from a static context
System.out.println("Umfang: " +Circle.getCircumference());
^
CircleExercise.java:10:47: non-static method getArea() cannot be referenced from a static context
System.out.println("Flaeche: " +Circle.getArea());
^
CircleExercise.java:15:46: non-static method getRadius() cannot be referenced from a static context
System.out.println("Radius: " +Circle.getRadius());
^
CircleExercise.java:16:46: non-static method getCircumference() cannot be referenced from a static context
System.out.println("Umfang: " +Circle.getCircumference());
^
CircleExercise.java:17:47: non-static method getArea() cannot be referenced from a static context
System.out.println("Flaeche: " +Circle.getArea());
^
CircleExercise.java:22:46: non-static method getRadius() cannot be referenced from a static context
System.out.println("Radius: " +Circle.getRadius());
^
CircleExercise.java:23:46: non-static method getCircumference() cannot be referenced from a static context
System.out.println("Umfang: " +Circle.getCircumference());
^
CircleExercise.java:24:47: non-static method getArea() cannot be referenced from a static context
System.out.println("Flaeche: " +Circle.getArea());
^
.\Circle.java:5:9: cannot find symbol
symbol : variable radius
location: class Circle
radius = rad;
^
.\Circle.java:10:35: cannot find symbol
symbol : variable radius
location: class Circle
return 2 * SomeMaths.PI * radius;
^
.\Circle.java:15:31: cannot find symbol
symbol : variable radius
location: class Circle
return SomeMaths.PI * radius * radius;
^
.\Circle.java:15:40: cannot find symbol
symbol : variable radius
location: class Circle
return SomeMaths.PI * radius * radius;
^
.\Circle.java:20:16: cannot find symbol
symbol : variable radius
location: class Circle
return radius;
^
.\Circle.java:25:9: cannot find symbol
symbol : variable radius
location: class Circle
radius = rad;
^
.\Circle.java:30:32: cannot find symbol
symbol : variable radius
location: class Circle
return "Kreis(Radius="+radius+")";
^
16 errors
|
|
|
| #14 (permalink) | |
|
Stammbenutzer
Team RPGenesis
Viertel Megabyte Registriert seit: 20.03.2008
Fachbeiträge: 409
Abgegebene Danke: 1
Erhielt 5 Danke für 5 Beiträge
|
Nein,
du darfst nicht Circle.getRadius() usw. schreiben, sondern c1.getRadius() usw. weil du ja von den jeweiligen Objekten die eigenschaften willst |
|
|
| #15 (permalink) | |
|
Neuer Benutzer
short
Themenstarter
Registriert seit: 09.04.2008
Fachbeiträge: 16
Abgegebene Danke: 0
Erhielt 0 Danke für 0 Beiträge
|
Kannst du das in den Code integrieren? Ich weiß grad nicht wirklich wo ich das hinpacken soll und was ich damit ersetzen soll.
// Jetzt hast du editiert.. ![]() So? Code:
public class CircleExercise
{
public static void main( String[] args )
{
Circle c1 = new Circle( 3.0 );
System.out.println("1. Kreis");
System.out.println("Radius: " +c1getRadius());
System.out.println("Umfang: " +c1.getCircumference());
System.out.println("Flaeche: " +c1.getArea());
Circle c2 = new Circle( 8.0 );
System.out.println("2. Kreis");
System.out.println("Radius: " +c2.getRadius());
System.out.println("Umfang: " +c2.getCircumference());
System.out.println("Flaeche: " +c2.getArea());
Circle c3 = new Circle( 5.0 );
System.out.println("3. Kreis");
System.out.println("Radius: " +c3.getRadius());
System.out.println("Umfang: " +c3.getCircumference());
System.out.println("Flaeche: " +c3.getArea());
}
}
|
|
|
| #16 (permalink) | |
|
Stammbenutzer
Team RPGenesis
Viertel Megabyte Registriert seit: 20.03.2008
Fachbeiträge: 409
Abgegebene Danke: 1
Erhielt 5 Danke für 5 Beiträge
|
**** sry,
hab was vergessen Code:
public class Circle
{
private double radius;
public Circle( double rad )
{
radius = rad;
}
public double getCircumference()
{
return 2 * SomeMaths.PI * radius;
}
public double getArea()
{
return SomeMaths.PI * radius * radius;
}
public double getRadius()
{
return radius;
}
public void setRadius( double rad )
{
radius = rad;
}
public String toString()
{
return "Kreis(Radius="+radius+")";
}
}
|
|
|
| #17 (permalink) | |
|
Stammbenutzer
Team RPGenesis
Viertel Megabyte Registriert seit: 20.03.2008
Fachbeiträge: 409
Abgegebene Danke: 1
Erhielt 5 Danke für 5 Beiträge
|
Code:
public class CircleExercise
{
public static void main( String[] args )
{
Circle c1 = new Circle( 3.0 );
System.out.println("1. Kreis");
System.out.println("Radius: " +c1.getRadius());
System.out.println("Umfang: " +c1.getCircumference());
System.out.println("Flaeche: " +c1.getArea());
Circle c2 = new Circle( 8.0 );
System.out.println("2. Kreis");
System.out.println("Radius: " +c2.getRadius());
System.out.println("Umfang: " +c2.getCircumference());
System.out.println("Flaeche: " +c2.getArea());
Circle c3 = new Circle( 5.0 );
System.out.println("3. Kreis");
System.out.println("Radius: " +c2.getRadius());
System.out.println("Umfang: " +c2.getCircumference());
System.out.println("Flaeche: " +c2.getArea());
}
}
|
|
|
| #18 (permalink) | |
|
Neuer Benutzer
short
Themenstarter
Registriert seit: 09.04.2008
Fachbeiträge: 16
Abgegebene Danke: 0
Erhielt 0 Danke für 0 Beiträge
|
Es funktioniert!!
Das spuckt die CMD aus: Code:
1. Kreis Radius: 3.0 Umfang: 18.849539999999998 Flaeche: 28.274309999999996 2. Kreis Radius: 8.0 Umfang: 50.26544 Flaeche: 201.06176 3. Kreis Radius: 5.0 Umfang: 31.4159 Flaeche: 78.53975 E:\Java\Kapitel 5 - Klassen, Attribute, Methoden>Pause Drücken Sie eine beliebige Taste . . . Tausend dank an dich
|
|
|
|
| Themen-Optionen | Thema durchsuchen |
| Ansicht | |
Ähnliche Themen
|
||||
| Thema | Autor | Forum | Antworten | Letzter Beitrag |
| Methoden Problem | Java-Problems | Java Basics - Anfänger-Themen | 13 | 04.02.2009 10:48 |
| Methoden Problem | Anfänger007 | Java Basics - Anfänger-Themen | 6 | 30.11.2006 15:02 |
| Problem mit Methoden | ivanstojkovic | Allgemeine Java-Themen | 7 | 24.02.2005 20:39 |
| Problem mit Methoden | Java Basics - Anfänger-Themen | 9 | 13.01.2005 07:02 | |
| Methoden Problem! | Java Basics - Anfänger-Themen | 1 | 10.12.2003 15:46 | |
| Lesezeichen |
|
|