In: Computer Science
write program in java
Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.
Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field.
Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA. When you prompt for ID and GPA, don’t let the user proceed until an ID between 10001 and 99999 and GPA between 2.0 and 4.0 ha been entered. After a valid student object has been created, save the information of a student to the file named StudentInformation.txt with each field separated by tab space. The file should have at least 4 records.
Create a method name readData that reads the StudentInformation.txt and displays each record along with the line number. Display headings to display the student information. Console sample output is given below. Demonstrate all the methods work correctly.
Create a class named RecusiveMethod having a recursive method. The recursive method accepts an integer argument and displays all even numbers from 1 up to the number passed as an argument. For example, if 25 is passed as an argument, the method will display 2, 4, 6, 8, 10, 12, 14, ……24. Demonstrate the method.
PersonalDetails.java
class PersonalDetails {
private String name, address;
public PersonalDetails(String name, String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
}
Student.java
class Student {
private int id;
private PersonalDetails personalDetails;
private String major;
private float GPA;
public Student(int id, PersonalDetails personalDetails, String major, float GPA) {
this.id = id;
this.personalDetails = personalDetails;
this.major = major;
this.GPA = GPA;
}
public int getId() {
return id;
}
public PersonalDetails getPersonalDetails() {
return personalDetails;
}
public String getMajor() {
return major;
}
public float getGPA() {
return GPA;
}
}
StudentApp.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.StringBuilder;
public class StudentApp {
//File name
public static final String FILE_NAME = "StudentInformation.txt";
public static void main(String[] args) {
try {
//Initialize a Reader to take input
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please Enter Student Details: ");
System.out.println("Enter the ID: ");
int id = 10000;//We initialized with 10000 because we know id starts from 10000
//Don't let user proceed until a ID between 10001 and 99999 is entered
do {
System.out.println("Please enter an ID between 10001 and 99999");
id = Integer.parseInt(reader.readLine());
} while(validateID(id));
System.out.println("Enter Name: ");
String name = reader.readLine();
System.out.println("Enter the address: ");
String address = reader.readLine();
System.out.println("Enter major");
String major = reader.readLine();
System.out.println("Enter GPA: ");
float gpa = 1.99;
//Don't let user proceed until a gpa between 2.0 to 4.0 is entered
do {
System.out.println("Please Enter GPA between 2.0 to 4.0");
gpa = Float.parseFloat(reader.readLine());
} while(validateGPA(gpa));
//Create the PersonalDetails object
PersonalDetails personalDetails = new PersonalDetails(name, address);
//Create the Student object
Student student = new Student(id, personalDetails, major, gpa);
//Close the reader
reader.close();
//Write the object to StudentInformation.txt file
//Initialize a writer
BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME));
//Initialize a stringbuilder for the fields
StringBuilder builder = new StringBuilder();
builder.append(student.getId());
builder.append("\t");//Tab space
builder.append(student.getPersonalDetails().getName());
builder.append("\t");
builder.append(student.getPersonalDetails().getAddress());
builder.append("\t");
builder.append(student.getMajor());
builder.append("\t");
builder.append(student.getGPA());
builder.append("\n");//New Line for next write
//Write to file
writer.write(builder.toString());
//Close the writer
writer.close();
} catch(IOException e) {
System.out.println("There was a problem in reading input or writing to file");
}
}
public void readData() {
try {
//Initialize the reader
BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME));
//Print Headings
System.out.println("ID\tName\tAddress\tMajor\tGPA");
String line;
//Read each line
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
//Close the reader
reader.close();
} catch(IOException e) {
System.out.println("Problem reading file");
}
}
}
RecursiveMethod
You can also use stack without the extra helper method
public class RecursiveMethod {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
int n = scanner.parserInt();
recursiveMethod(n);
} catch (Exception e) {
System.out.println("Error);
}
}
private static void recursiveMethod(int n) {
helper(2, n);
}
private static helper(int i, int n) {
if (i > n)
return;
System.out.print(i + " ");
helper(i+2, n);
}
}