207k views
0 votes
You have the templates of 2 classes, Person and Program. The Person class has 4 attributes, name, age, major and gpa. There is a constructor for the class that is used to make Person objects. There are setter/mutator methods and getter/accessor methods for those attributes. There is also a printInfo() method that prints out information about the person. The Program class has a main method that has 5 Person objects person1 to person5 declared and initialized. The main method within the class also has a Scanner object that is used to take input for a person number and a newGPA. Your task is as follows:

a. Based on the input of personSelect select the appropriate person and change their GPA to the newGPA input and then print out their information. For instance if the user types in 1 then select person 1 and print out their information using the printInfo method. Do it for all possible inputs from 1 to 5.
b. If however the input is not 1 to 5 then inform the user they have to have to type in an appropriate person number

Person.java

public class Person {
String name;
int age;
double gpa;
String major;
public Person(String aName, int aAge, String aMajor, double aGpa)
{
name = aName;
age = aAge;
gpa = aGpa;
major = aMajor;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String pname) {
name = pname;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int page) {
age = page;
}
/**
* @return the gpa
*/
public double getGpa() {
return gpa;
}
/**
* @param gpa the gpa to set
*/
public void setGpa(double pgpa) {
gpa = pgpa;
}
/**
* @return the major
*/
public String getMajor() {
return major;
}
/**
* @param major the major to set
*/
public void setMajor(String pmajor) {
major = pmajor;
}
public void printInfo()
{
System.out.println("#########################");
System.out.println("Business College ");
System.out.println("Name: " + name);
System.out.println("Major: " + major);
System.out.println("GPA: " + gpa);
System.out.println("#########################");
}
}
Program.java--------------------------------------------------------------------------------

import java.util.Scanner;
public class Program {
public static void main(String[] args) {
//Declare person objects here
Person person1 = new Person("Dewars",40 , "MIS", 3.9);
Person person2 = new Person("Budweiser",23 , "MIS", 3.2);
Person person3 = new Person("Appletons",25 , "MIS", 3.0);
Person person4 = new Person("Beam",20 , "Finance", 3.7);
Person person5 = new Person("Daniels",19 , "Accounting", 2.9);
Scanner scan = new Scanner(System.in);
System.out.println("Type in a person number whose gpa you would like to change > ");
int personSelect = scan.nextInt();
System.out.println("Type in the gpa you would like the person's to be > ");
double newGPA = scan.nextDouble();
/*
* CODE HERE
*/
}
}

1 Answer

6 votes

Answer:

Hi, I'm going to put the code answer here and you put in the corresponding line to not copy all the code in the answer.

replace the following line or adjust to the following code

/*

* CODE HERE

while(personSelect <= 0 || personSelect > 5) {

System.out.println("Wrong number, try to input the number in range 1 to 5" );

personSelect = scan.nextInt();

}

if(personSelect == 1){

person1.setGpa(newGPA);

printInfo()

}

else if(personSelect == 2){

person2.setGpa(newGPA);

printInfo()

}

else if(personSelect == 3){

person3.setGpa(newGPA);

printInfo()

}

else if(personSelect == 4){

person4.setGpa(newGPA);

printInfo()

}

else {

person5.setGpa(newGPA);

printInfo()

}

*/

Step-by-step explanation:

According to the description of code, we have to add some lines to resolve the questions.

a):

In base on the input, we have to modify the attribute GPA with the method setGpa depending on the person chosing. We call the person chosen before and also call the method setGpa( ) and pass as parameter the GPA value obtained in tha last input

b)

In this case we have to create a loop for iterate the times that is necesary to get a value of person that is permit in range 1 to 5, and hence that we create and individual if condition to assign the GPA to the person chosen.

I hope it's help you.

User Khb
by
5.3k points