Question

In: Computer Science

write program in java Create a class named PersonalDetails with the fields name and address. The...

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.

Solutions

Expert Solution

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);
    }
}

Related Solutions

(Java) Design a class named Person with fields for holding a person’s name, address, and telephone...
(Java) Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator and...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone number (all Strings) –Write a constructor that takes all the required information. –Write a constructor that only takes the name of the person and uses this to call the first constructor you wrote. –Implement the accessor and mutator methods. Make them final so subclasses cannot override them –Implement the toString() method •Design a class named Customer, which extends the Person class. It should have...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
Design a class named Person with fields for holding a person's name, address, and telephone number(all...
Design a class named Person with fields for holding a person's name, address, and telephone number(all as Strings). Write a constructor that initializes all of these values, and mutator and accessor methods for every field. Next, design a class named Customer, which inherits from the Person class. The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes these...
Design a class named Person with fields for holding a person's name, address, and telephone number(all...
Design a class named Person with fields for holding a person's name, address, and telephone number(all as Strings). Write a constructor that initializes all of these values, and mutator and accessor methods for every field. Next, design a class named Customer, which inherits from the Person class. The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes these...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
This is 1 java question with its parts. Thanks! Create a class named Lease with fields...
This is 1 java question with its parts. Thanks! Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT