JAVA intro to java. Class User This class will create a user that will hold four
ID: 670091 • Letter: J
Question
JAVA intro to java.
Class User
This class will create a user that will hold four variables:
-Name(String)
-Username(String)
-Age(int)
-Score(int)
This should have a setter and getter for each variable, you should also have a constructor for the User class.
Class ScoreTest
This class should take in two scores and compare them, returning just the highest score.
-Score1(int)
-Score2(int)
+Winner(int)
This should have one method in it to compare the two scores and return the highest score.
MAIN
The main class should create two Users and ask for input with a Scanner, then it should compare the two users and see who has the highest score.
+User1(User)
+User2(User)
+Winner(ScoreTest)
This should then print out the winner in this manner.
The winner is Darren, winning with a score of 1000!!!!!
Explanation / Answer
import java.util.Scanner;
public class ScoreTest {
public static void main(String []args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter User 1 details");
System.out.println("Enter Name");
String n1 = scan.next();
System.out.println("Enter User Name");
String un1 = scan.next();
System.out.println("Enter Age(years)");
int age1 = scan.nextInt();
System.out.println("Enter Age(years)");
int sc1 = scan.nextInt();
User user1 = new User(n1, un1, age1, sc1);
System.out.println("Enter User 2 details");
System.out.println("Enter Name");
String n2 = scan.next();
System.out.println("Enter User Name");
String un2 = scan.next();
System.out.println("Enter Age(years)");
int age2 = scan.nextInt();
System.out.println("Enter Score");
int sc2 = scan.nextInt();
User user2 = new User(n2, un2, age2, sc2);
ScoreTest st = new ScoreTest();
//Use one of the following two methods
int highest = st.compare(user1.getScore(), user2.getScore());
System.out.println("The winner is "+(highest == user1.getScore() ? user1.getName() : user2.getName())+", winning with a score of "
+(highest == user1.getScore() ? user1.getScore() : user2.getScore())+"!!!!!");
//Or use the following method
User winner = st.compare(user1, user2);
System.out.println("The winner is "+winner.getName()+", winning with a score of "+winner.getScore()+"!!!!!");
}
public int compare(int score1, int score2) {
if(score1 > score2)
return score1;
else
return score2;
}
public User compare(User user1, User user2) {
int highest = this.compare(user1.getScore(), user2.getScore());
if(highest == user1.getScore())
return user1;
else
return user2;
}
}
public class User {
private String name;
private String userName;
private int age;
private int score;
public User() {
name = null;
userName = null;
age = 0;
score = 0;
}
public User(String name, String userName, int age, int score) {
this.name = name;
this.userName = userName;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.