Hi all, in reference data type i have two person with two age and i want to compare their ages with return how can i do that? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Hi all, in reference data type i have two person with two age and i want to compare their ages with return how can i do that?

class Person { private string name; private int age; person (string n) { this.name = n; } public int getAge(){ return Age ; . . . i want use static int max ( int a, int b) { // i don't know is it true or not if ( a>b) { return a } else { return b} how can i do that?

13th Nov 2016, 4:23 PM
ehsan shahbazi
ehsan shahbazi - avatar
5 Answers
+ 2
Here you go Arsal: public class Main { public static void main(String[] args) { Person j; Person o; o = new Person("john", 18); j = new Person ("bob", 24); // birthday(j); //good(o); System.out.println("name of two persons :"); System.out.println("bob" + " " + j.getAge()); System.out.println("john" + " " + o.getAge()); System.out.println ("The older person is: " + j.getOlderPerson(o).getName()); System.out.println("His age is: " + j.getOlderAge(o)); } } class Person { private String name; private int age; Person (String nm, int age) { this.name = nm; this.age = age; } public String getName(){ return this.name; } public int getAge(){ return age; } public void setAge(int age){ this.age = age; } public void setName(String name){ this.name = name; } Person getOlderPerson(Person p){ if(this.age > p.age) return this; return p; } int getOlderAge(Person p){ if(this.age > p.age) return this.age; return p.age; } }
15th Nov 2016, 10:08 AM
Arsal Ali
Arsal Ali - avatar
+ 2
hi, please help me, what is the problem? public class Vehicle { private String color; private String module; private int speed; private double fuel; Vehicle() { this.setColor("red"); } Vehicle(String c) { this.setColor(c); } Vehicle(String m, int s){ this.speed = s; this.module = m; } public String getColor() { return color; } public void setColor(String c) { this.color= c; } public String getModul() { return module; } public void setModul(String m) { this.module = m; } public int getSpeed() { return speed; } public void setSpeed(int s) { this.speed = s; } public double getFuel() { return fuel; } public void setFuel(double f) { this.fuel = f; } public void bogh() { System.out.println("beep"); } Vehicle getfastercarmodule(Vehicle x){ if(this.speed > x.speed) return this; return x; } int getfastercar(Vehicle x){ if(this.speed > x.speed) return this.speed; return x.speed; } } class Program { public static void main(String [] args) { Vehicle j; Vehicle o; j = new Vehicle("benz", 300); o = new Vehicle("bmw", 250); System.out.println("the faster car is"+" "+ j.getfastercarmodule (o).getModule()); System.out.println("the speed of car is"+ " "+j.getfastercar(o)); Vehicle car1 = new Vehicle(); Vehicle car2 = new Vehicle("green"); Vehicle car3 = new Vehicle(); System.out.println("the color for car1 and car3 is" + " " + car1.getColor()+ " " + "and the color for car2 is " + car2.getColor()); } }
24th Nov 2016, 4:42 PM
ehsan shahbazi
ehsan shahbazi - avatar
+ 1
Why would you make this method static? You are using instance variables (variables that are actively used in this object). So here my suggestion: public int getOlderPerson(Person p) { if(this.getAge()>p.getAge) return this.getAge(); return p.getAge(); } By making in It an instance method, you can call this function like this. Person per, pers; //initialize them int maxAge = per.getOlderPerson(); The This calls the The object on which you call your method.
14th Nov 2016, 10:57 PM
Arsal Ali
Arsal Ali - avatar
+ 1
it wasn't work, i wrote my code can you help me again ty public class Program { public static void main(String[] args) { Person j; Person o; o = new Person ("john"); j = new Person ("bob"); j.setAge(24); o.setAge(18); birthday(j); good(o); System.out.println("name of two persons :"); System.out.println("bob" + " " + j.getAge()); System.out.println("john" + " " + o.getAge()); } static void birthday (Person p) { p.setAge(p.getAge() + 2); } static void good (Person d) { d.setAge(d.getAge() + 6); } } class Person { private String name; private int age; Person (String n) { this.name = n; } public int getAge() { return age; } public void setAge(int a) { this.age = a; } }
15th Nov 2016, 7:34 AM
ehsan shahbazi
ehsan shahbazi - avatar
0
thank you it was very good
15th Nov 2016, 4:59 PM
ehsan shahbazi
ehsan shahbazi - avatar