StudentLoans class
3 Code a method named createStudent(). This method has no parameters, but returns a Student object.
a) Define a Scanner variable to read from the keyboard.
b) Define variables for name, student number and loan amount. Prompt the user to type data values for each of the variables and then read the values from the keyboard.
c) Define and create a Student object from the data read from the keyboard.
d) Return this Student object.
4 Code a method named createPostGraduate(). This method has no parameters, but returns a PostGraduate object.
a. Define a Scanner variable to read from the keyboard.
b. Define variables for name, student number and loan amount and thesis. Prompt the user to type data values for each of the variables and then read the values from the keyboard.
c. Define and create a PostGraduate object from the data read from the keyboard.
d. Return this PostGraduate object.
import java.util.Scanner;
public class StudentLoans {
public Student createStudent() {
Scanner in = new Scanner(System.in);
System.out.println("Name:");
String name = in.nextLine();
System.out.println("Student number:");
String studentNumber = in.nextLine();
System.out.println("Loan amount:");
double loanAmount = Double.parseDouble(in.nextLine());
return new Student(name, studentNumber, loanAmount);
}
public PostGraduate createPostGraduate() {
Scanner in = new Scanner(System.in);
System.out.println("Name:");
String name = in.nextLine();
System.out.println("Student number:");
String studentNumber = in.nextLine();
System.out.println("Loan amount:");
double loanAmount = Double.parseDouble(in.nextLine());
System.out.println("Thesis:");
String thesis = in.nextLine();
return new PostGraduate(name, studentNumber, loanAmount, thesis);
}
}
Comments
Leave a comment