Define a class Athelete which has attributes of type String Name, Team and Sport
ID: 3887379 • Letter: D
Question
Define a class Athelete which has attributes of type String Name, Team and Sport. Has an integer attribute Uniform number and a double attribute Salary. It has a no parameter constructor and a five attribute constructor. It also has getters and setters for all its attributes. It also has a method called Raise which takes a double parameter which represent a percentage. The method changes the athletes salary by giving him the percentage increase in salary.
Write a main program that declares two variables of type Athlete and allows the user to enter the values for each Athlete, once using the five variable constructor and once using the no variable constructor and the setters. Have the program display the athletes and their values and then prompt the user for a percentage raise amount for each athlete and prints out their new salaries.
Explanation / Answer
import java.io.*;
import java.util.*;
class Athelete {
private String name;
private String team;
private String sport;
private int uni_no;
private double salary;
public Athelete(String nm, String tm, String sp, int un, double sal){
name = nm;
team = tm;
sport = sp;
uni_no = un;
salary = sal;
}
public void Raise(double per){
salary = salary + (per/100)*salary;
}
public void disp(){
System.out.println("Name:" + name);
System.out.println("Team:" + team);
System.out.println("Sport:" + sport);
System.out.println("Uniform Number:" + uni_no);
System.out.println("Salary:" + salary);
}
public void setName(String a){
name = a;
}
public void setTeam(String a){
team = a;
}
public void setSport(String a){
sport = a;
}
public void setUniformNumber(int a){
uni_no = a;
}
public void setSalary(double a){
salary = a;
}
public String getName(){
return name;
}
public String getTeam(){
return team;
}
public String getSport(){
return sport;
}
public int getUniformNumber(){
return uni_no;
}
public double getSalary(){
return salary;
}
}
public class DemoAthelete {
public static void main(String[] args){
String name;
String team;
String sport;
int uniform_number;
double salary;
Scanner sc = new Scanner(System.in);
System.out.println("Name:");
name = sc.next();
System.out.println("Team:");
team = sc.next();
System.out.println("Sport:");
sport = sc.next();
System.out.println("Uniform Number:");
uniform_number = sc.nextInt();
System.out.println("Salary:");
salary = sc.nextDouble();
Athelete a1 = new Athelete(name,team,sport,uniform_number,salary);
System.out.println("Name:");
name = sc.next();
System.out.println("Team:");
team = sc.next();
System.out.println("Sport:");
sport = sc.next();
System.out.println("Uniform Number:");
uniform_number = sc.nextInt();
System.out.println("Salary:");
salary = sc.nextDouble();
Athelete a2 = new Athelete(name,team,sport,uniform_number,salary);
a1.disp();
a2.disp();
System.out.println("Enter raise for athelete 1:");
double per1 = sc.nextInt();
a1.Raise(per1);
a1.disp();
System.out.println("Enter raise for athelete 2:");
double per2 = sc.nextInt();
a2.Raise(per2);
a2.disp();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.