Qu`affiche le programme suivante? - BFH

Transcription

Qu`affiche le programme suivante? - BFH
Java-Prüfungsaufgaben
exercices d'examen en java

Was gibt folgendes Programm aus?
Qu'affiche le programme suivante?
public class Auto {
private String mark;
private String color;
private float price;
public Auto(String mark, String color) {
this.mark = mark;
this.color = color;
price=0;
}
public Auto(String mark, String color,float price) {
this.mark = mark;
this.color = color;
this.price = price;
}
public void setPrice(float price)
{
this.price=price;
}
@Override
public String toString() {
return "[" + mark + ":" + color + ":" + price + ".-]";
}
}
public class Pruefungsaufgaben {
public static void main(String[] args) {
Auto A1=new Auto("VW","pink",23900.0);
Auto A2=new Auto("BMW","silver");
A2.setPrice(78000.0);
System.out.println(A1 +"\n" + A2);
}
}
[VW:pink:23900.0.-]
[BMW:silver:78000.0.-]

Überladen sie die Methode setPrice(...) in der Klasse Auto so, dass man den Preis mit
einer Integer-Zahl setzen kann.
Surchargez la méthode setPrice(...) de la façon qu'on puisse mettre le pris avec une
valeur de type integer.

Schreiben sie einen Default-Konstruktor (ohne Parameter) für die Klasse Auto welcher die
Werte mark,color und price auf "noname","nocolor" und 0.0 setzt.
Écrivez pour la classe Auto un constructeur sans paramétrés qui mets les membres mark,
color et price à "noname","nocolor" et 0.0.
Schreiben sie beide Methoden hier:
Écrivez les 2 méthodes ici:
public void setPrice(int price)
{
this.price= (int)price;
}
public Auto()
{
this.mark = "noname";
this.color = "nocolor";
price=0.0;
}

Welche 5 Fehler enthält folgendes Programm, bitte markieren und begründen!
Trouvez les 5 erreur dans le programme suivant. Marquez et expliquez, S.V.P.!
public class Pruefungsaufgaben {
public static void main(String[] args) {
Auto A1=new Auto("VW","black",45000.0f);
Auto A2=new Auto("BMW","silver");
Auto A3=new Auto(); <-this constructor doesn't exist!
A2.setPrice(78000); <-setPrice(...) expects float and not int value!
int p=A3.getPrice();<-this methode isn't there in class Auto
System.out.println("Mark: " + A2.mark);
^- private member!
A2.Auto("Jaguar","black");<-constructor can't be called at this place!
}
}

Leiten sie eine Klasse FlyingCar von Auto ab. Fügen sie dieser neuen Klasse den Member
maxAltitude vom Typ int hinzu und schreiben sie nur einen einzigen Konstruktor, welcher
alle Member initialisiert. Überschreiben sie dann die toString() Methode so dass sie
folgende Ausgabe macht: [<mark>:<color>:<price>: < maxAltitude>]
(Hinweis: die Texte in spitzen Klammern sind Variablen)
Héritez une classe FlyingCar de la classe Auto. Ajoutez à cette nouveau classe le membre
maxAltitude de type int. Écrivez un seul constructeur qui initialise toutes les membres.
Ensuite récrivez la méthode toString() afin qu'elle affiche un objet comme suit:
[<mark>:<color>:<price>: < maxAltitude>]
(remarques: les textes entre chevrons sont des variables)
public class FlyingCar extends Auto{
private int maxAltitude;
public FlyingCar(String mark,String color,float price,int maxAltidude)
{
super(mark,color,price);
this.maxAltitude=maxAltitude;
}
public String toString() {
String t=super.toString();
return t.substring(0,t.length()-1) + //cuts off last character->']'
":" + maxAltitude + "]";
/*other possibility:
return "[" + mark + ":" + color + ":" +
price + ".-" + ":" + maxAltitude + "]";
IMPORTANT: does only work if the member in
class "Auto" are changed to protected!!!
*/
}
}