Using Java import data from a text file named fata.txt then create two Student o
ID: 673178 • Letter: U
Question
Using Java import data from a text file named fata.txt then create two Student objects with the following name(String), id(String), and gpa(double values:
S1: John, 1111, 3.5
S2: Jean,2222, 3.9
The Student class must be a subclass of a Person class. The Person class must have a name field and an id field and the Student class must have a gpa field. THe Student class must also implement an interface called CurveGPA. The interface forces the Student class to implement a curveGPA() method that would add to each exisiting GPA.
Display these two objects with the curved GPA values on the console using toString() method.
Explanation / Answer
public class Person {
String name,id;
public Person(String name, String id) {
this.name = name;
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Person [name=" + name + ", id=" + id + "]";
}
}
Student.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
public class Student extends Person implements CurveGPA {
public Student(String name, String id) {
super(name, id);
}
double gpa;
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Student [gpa=" + gpa + ", name=" + name + ", id=" + id + "]";
}
@Override
public void curveGPA(double gpa) {
this.gpa=gpa;
}
public static void main(String args[]){
try {
File file = new File("fata.txt");
Scanner scanner = new Scanner(file);
String line1=scanner.nextLine().split(":")[1].trim();
String name1=line1.split(",")[0];
String id1=line1.split(",")[1];
double gpa1=Double.parseDouble(line1.split(",")[2]);
Student student1=new Student(name1, id1);
student1.curveGPA(gpa1);
String line2=scanner.nextLine().split(":")[1].trim();
String name2=line2.split(",")[0];
String id2=line2.split(",")[1];
double gpa2=Double.parseDouble(line2.split(",")[2]);
Student student2=new Student(name2, id2);
student2.curveGPA(gpa2);
System.out.println("Student 1: "+student1);
System.out.println("Student 2: "+student2);
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
fata.txt :
S1: John, 1111, 3.5
S2: Jean,2222, 3.9
output :
Student 1: Student [gpa=3.5, name=John, id= 1111]
Student 2: Student [gpa=3.9, name=Jean, id=2222]
output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.