Monday, 15 September 2014

java - get a different variable from a calculated class -



java - get a different variable from a calculated class -

i new @ java becoming more , more addicted. created these classes:

car car1 = new car(); car1.model = "toyota"; car1.price = 1500; auto car2 = new car(); car2.model = "peugeot"; car2.price = 2600;

i calculated max cost with

int maxprice=math.max(car1.price, car2.price);

which peugeot.

now want print model of calculated price:

system.out.println("the expensive auto "+ ????);

here problem. can't figure out goes here. thought .getmodel or can't work. give thanks in advance help. thanks.

this best solution you:

public class main { public static void main() { auto car1 = new car(); car1.setmodel ( "toyota" ); car1.setprice ( 1500 ); auto car2 = new car(); car2.setmodel ( "peugeot" ); car2.setprice ( 2600 ); int biggestprice = math.max ( car1.getprice(), car2.getprice() ) string model = ""; switch ( biggestprice ) { case car1.getprice: model = car1.getmodel(); break; case car2.getprice: model = car2.getmodel(); break; default: assert false : "got number should not able appear"; system.out.println ( "error!" ); return; } system.out.println("the expensive auto " + model); } class auto { string model; int price; public auto () {} public auto ( string model, int cost ) { this.model = model; this.price = price; } public void getmodel ( ) { homecoming this.model; } public void setmodel (string model ) { this.model = model; } public void getprice ( ) { homecoming this.price; } public void setprice ( int cost ) { this.price = price; } } }

hope helped bit :)

java class

No comments:

Post a Comment