JAVA 1) public class FileConstructorExample { /** * @param args the command line
ID: 3739756 • Letter: J
Question
JAVA
1)
public class FileConstructorExample {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//Task #1 - See Domain class
//Task #2 - See Domain class
//Task #3
//Create a method that will read the input file, create a Student object, and print the student object
//Task #4
//Create a method that will ask the user for a new first name, last name, panther id, and gpa of another student
//In the same method, save the information into the same file, overlaying the data that was there before.
//Task #5
//Run the program & check what the file looks like.
}
}
2)
package fileconstructorexample;
public class Student {
String lastName, firstName, pantherId;
double gpa;
//Task 1:
//Define a DEFAULT Constructor that initializes gpa to 1.0, and the rest to blanks
//Task 2:
//Define a NON-Default Constructor that received parameters for every instance variable,
//& initializes each variable with the corresponding parameter
public String getLastName() {
return lastName;
}
public String getFirstName() {
return firstName;
}
public String getPantherId() {
return pantherId;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setPantherId(String pantherId) {
this.pantherId = pantherId;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public double getGpa() {
return gpa;
}
@Override
public String toString() {
return "Student{" + "lastName=" + lastName + ", firstName=" + firstName + ", pantherId=" + pantherId + ", gpa=" + gpa + '}';
}
}
Explanation / Answer
1)
public class FileConstructorExample {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//Task #1 - See Domain class
//Task #2 - See Domain class
//Task #3
//Create a method that will read the input file, create a Student object, and print the student object
File file = new File(args[0]);
Scanner sc = new Scanner(file);
Student s = new Student(sc.next(), sc.next(), sc.next(), sc.nextDouble());
System.out.println(s.toString());
//Task #4
//Create a method that will ask the user for a new first name, last name, panther id, and gpa of another student
//In the same method, save the information into the same file, overlaying the data that was there before.
void writeStudent(){
System.out.println("Enter student information: LastName FirstName pantherID GPA ");
String lnm,fnm,pid;
double gp;
lnm = sc.next();
fnm = sc.next();
pid = sc.next();
gp = sc.nextDouble();
DataOutputStream dos = new DataOutputStream(new FileOutputStream(args[0]));
dos.writeBytes(lnm);
dos.writeBytes(fnm);
dos.writeBytes(pid);
dos.writeDouble(gp);
dos.close();
}
//Task #5
//Run the program & check what the file looks like.
}
}
2)
package fileconstructorexample;
public class Student {
String lastName, firstName, pantherId;
double gpa;
//Task 1:
//Define a DEFAULT Constructor that initializes gpa to 1.0, and the rest to blanks
Student(){
this.lastName = "";
this.firstName = "";
this.pantherId = "";
this.gpa = 1.0;
}
//Task 2:
//Define a NON-Default Constructor that received parameters for every instance variable,
//& initializes each variable with the corresponding parameter
Student(String lname; String fname; String pid, double g){
this.lastName = lname;
this.firstName = fname;
this.pantherId = pid;
this.gpa = g;
}
public String getLastName() {
return lastName;
}
public String getFirstName() {
return firstName;
}
public String getPantherId() {
return pantherId;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setPantherId(String pantherId) {
this.pantherId = pantherId;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public double getGpa() {
return gpa;
}
@Override
public String toString() {
return "Student{" + "lastName=" + lastName + ", firstName=" + firstName + ", pantherId=" + pantherId + ", gpa=" + gpa + '}';
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.