Code question (java) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Code question (java)

At this code why don't I get true with my "fasterThan" method? public class dasd { public static void main (String args[]){ Vehicle v1 = new Vehicle(); Vehicle v2 = new Vehicle("motor",4); v1.setSpeed(3); System.out.println(v1.getName()); System.out.println(v1.getSpeed()); System.out.println(v2.getName()); System.out.println(v2.getSpeed()); v1.fasterThan(v2); } } class Vehicle{ private String name; private int speed; public String getName(){ return this.name; } public void setName(String c){ this.name = c; } public void setSpeed(int i){ this.speed = i; } public int getSpeed(){ return this.speed; } Vehicle(){ name = "car"; } Vehicle(String c, int b){ name = c; speed = b; } boolean fasterThan(Vehicle v){ if (this.speed>v.speed) return true; else return false; } }

28th Jul 2018, 6:41 AM
Adam
Adam - avatar
2 Answers
+ 1
The Solution is pretty simple: You have to use System.out.print() to print out the functions result:)
28th Jul 2018, 7:50 AM
Jonas Schröter
Jonas Schröter - avatar
+ 1
You have set 3 speed for v1 and 4 speed for v2; this.speed is a speed for v1 since you call a method for this object; v.speed is for the argument; 3 > 4 causes false
28th Jul 2018, 7:51 AM
Steppenwolf
Steppenwolf - avatar