In: Computer Science
Please finish the following JAVA Codes:
1. The class Person includes information of a person’s first name, last name, phone number, and email. Implement the class with appropriate constructors, getters and setters.
2. Derive a Student class that extends Person. In addition to a person’s information, a student has major, year, GPA, and a list of enrolled courses. In addition to appropriate constructors, getters and setters, make two methods addCourse and dropCourse.
3. Design and implement a program, either in main() or in another class, that calculates the GPA of a student. The student’s courses should be displayed in console, one at a time, and the grade of the course will be input to console by user. After all grades were input, the GPA should be displayed and be recorded in the student object.
hey i am done with the code .. here is the code ...
i had face a problem with major variable .. i dont know what the role of the major variable .. so i not include major variable in any calculation in the GPA .. you can change it on your own ..
here is the final code ---
Person.java
package studentgpa;
/**
*
* @author Aditya kumar
*/
public class Person {
String firstname , lastname ,email;
long mobileno ;
public Person(String firstname, String lastname, String email,
long mobileno) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.mobileno = mobileno;
}
public Person() {
}
//setters and getters
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getMobileno() {
return mobileno;
}
public void setMobileno(long mobileno) {
this.mobileno = mobileno;
}
}
StudentGPA.java
package studentgpa;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class StudentGpa extends Person{
int major ;
int year ;
List<String> course=new ArrayList<>(); //holds the
course
List<String> grade=new ArrayList<>(); //holds the
grade
public StudentGpa() {
}
//contructor with super constructor of Person Class
public StudentGpa(int major, int year, List<String> course,
String firstname, String lastname, String email, long mobileno)
{
super(firstname, lastname, email, mobileno);
this.major = major;
this.year = year;
this.course = course;
}
//setter and getters
public int getMajor() {
return major;
}
public void setMajor(int major) {
this.major = major;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public List<String> getCourse() {
return course;
}
public List<String> getGrade() {
return grade;
}
//add course method .... we also add the grade along with
course
public void addCourse(String coursename) {
course.add(coursename);
System.out.println("Add Grade of the course : ");
Scanner sc=new Scanner(System.in);
grade.add(sc.next());
}
public void dropCourse(String position)
{//for this first get the index of the subject from course and if
subject succefully removed
// we can put the index value into grade list to remove the related
grade
int index=course.indexOf(position);
boolean b=course.remove(position);
if(b)
{
grade.remove(index);
System.out.println("Successfully deleted ");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
StudentGpa studentGpa=new StudentGpa();
System.out.println("Enter Person's Details : ");
System.out.println("Enter First Name of Person : ");
Scanner sc=new Scanner(System.in);
studentGpa.setFirstname(studentGpa.firstname=sc.next());
System.out.println("Enter Person's Last Name ");
studentGpa.setLastname(studentGpa.lastname=sc.next());
System.out.println("Enter Person's phone number : ");
studentGpa.setMobileno(studentGpa.mobileno=sc.nextLong());
System.out.println("Enter Person's Email : ");
studentGpa.setEmail(studentGpa.email=sc.next());
System.out.println("Enter Person's major "); //here we assume major
as credit of the subject
studentGpa.setMajor(studentGpa.major=sc.nextInt());
System.out.println("Enter year : ");
studentGpa.setYear(studentGpa.year=sc.nextInt());
System.out.println("Enter the total number of course : ");
int coursenumber=sc.nextInt();
for (int i=1;i<=coursenumber;i++)
{
System.out.println("Enter "+i+" Course : ");
String coursename=sc.next();
studentGpa.addCourse(coursename);
}
// to drop the course
/*
System.out.println("Enter the subject");
String remove=sc.next();
studentGpa.dropCourse(remove);
*/
//print information
System.out.println( studentGpa.getFirstname());//first name
System.out.println(studentGpa.getLastname());//last name
System.out.println(studentGpa.getMobileno());//mobile no
System.out.println(studentGpa.getEmail());//email
System.out.println(studentGpa.getMajor());//major
System.out.println(studentGpa.getYear());//year
System.out.println(studentGpa.getCourse());//total subject
System.out.println(studentGpa.getGrade()); //all grades
float total =0;
int count=0;
String gradename="";
Iterator iterator = studentGpa.grade.iterator();
while(iterator.hasNext()) {
gradename=(String) iterator.next();
count++;
//calculate total gradepoints
switch(gradename)
{
case "A" : total=total+3*4; //3 is credit and 4 is Grade
value
break;
case "B" : total=(float) (total+3*3.3); //we assume 3 is the credit
and 3.3 is grade value
break;
case "C" : total=(float) (total+3*2.7); //we assume 3 is credit and
2.7 is grade value
default: System.out.println("No Such Grade ");
}
}
float OverallGPA=total/count;
System.out.println("Overall Grade is :"+OverallGPA);
//print overall GPA
}
}
and here is the snapshot of the output ---
i hope it was helpful for you ,. Please give me a like to appreciate my effort and work .. and feel free to comment regarding this question .. I am happy to help you . Thank YOu !