I am writing my code for this assignment but I keep having issues with my code. Can
Question:
I am writing my code for this assignment but I keep having issues with my code. Can someone shows me how to implement all the functionality. Thanks.
I am posting my code at the end but it's not working properly and It's not completed yet.
work with Scanner, a text file and toString().
- Student.java
- fields for first name, last name, id number, age, grade (a double) and pass (a Boolean)
- default constructor
- overloaded constructor for all fields except pass.
- Pass will be set based on the grade
- Grade of 60 or more will be set to true
- Grade under 60 will be set to false
- All parameters will be validated and throw IllegalArgumentException for an invalid value
- Pass will be set based on the grade
- Getters and setters will be provided for each field
- Setters will validate but will ignore bad parameters and make no change to the field
- toString() will be implemented
- Lab8Driver.java
- field HashMap students;
- Implements the main(...) method that will call each of two methods
- void readFromPrompt() will use scanner to allow users to enter student data when prompted. Data entered will be used to create Student objects one at a time and add them to the HashMap.
- Uses a do/while loop and a boolean to ask if you want to create a Student. "y" for yes, "n" for no.
- If "y" then the program will present a succession of prompts to enter Student data. If "n" then the Boolean will be changed to false and the method will terminate with a message.
- See sample hint and sample run below
- void readFromFile() will read data from the student_data.txt file (provided) and create Student objects and add them to the same HashMap above. Don't forget to close the Scanner when finished.
- void readFromPrompt() will use scanner to allow users to enter student data when prompted. Data entered will be used to create Student objects one at a time and add them to the HashMap.
- void showStudents()will display the contents of the HashMap to the console calling Student.toString() for each Student
HINT: do/while
boolean proceed = true;
do {
System.out.println("Do you wish to create a Student? (y/n): ");
String choice = scanner.next().toLowerCase();
if (choice.equals("y")) {
// Here you will prompt for data, create Students and add
// to the HashMap. There will be a separate prompt for // each piece of data.
} else {
proceed = false;
}
} while (proceed);
Sample runtime:
DEBUG: calling readFromPrompt
Do you wish to create a Student? (y/n):
y
Enter first name:
Nosaj
Enter last name:
Nosirrah
Enter id number:
A00123456
Enter age as a whole number:
40
Enter grade as a decimal number:
94.6
Do you wish to create a Student? (y/n):
n
Data entry finished!
DEBUG: calling readFromFile
List of Students created
Student [firstName=Pike, lasName=Bass, idNumber=A00953177, age=22, grade=81.5, pass=true]
Student [firstName=Steed, lasName=Mare, idNumber=A00553954, age=44, grade=78.7, pass=true]
Student [firstName=Buck, lasName=Doe, idNumber=A00654321, age=36, grade=69.9, pass=true]
Student [firstName=Sunny, lasName=McCloud, idNumber=A00559633, age=25, grade=88.8, pass=true]
Student [firstName=Robin, lasName=Crow, idNumber=A00112233, age=43, grade=73.9, pass=true]
Student [firstName=Nosaj, lasName=Nosirrah, idNumber=A00123456, age=40, grade=94.6, pass=true]
Student [firstName=Catfish, lasName=Sturgeon, idNumber=A00456123, age=78, grade=56.7, pass=false]
public class Student { /** * Declaring class instance variables */ private String firstName; private String lastName; private String idNumber; private int age; private double grade; private boolean pass; private final static double PASSMARKS = 60; /** * Default constructor * @param firstName * @param lastName * @param idNumber * @param age * @param grade * @param pass */ public Student(final String firstName, final String lastName, final String idNumber, final int age, final double grade, final boolean pass) throws IllegalUserInputArgumentException, IllegalInvalidIdNumberArgumentException, IllegalAgeArgumentException, IllegalGradeArgumentException { validateFirstName(firstName); setLastName(lastName); setIdNumber(idNumber); setAge(age); setGrade(grade); setPass(pass); this.firstName = firstName; this.lastName = lastName; this.idNumber = idNumber; this.age = age; this.grade = grade; this.pass = pass; } /** * Overloaded constructor for all fields except pass * @param firstName * @param lastName * @param idNumber * @param age * @param grade */ public Student(final String firstName, final String lastName, final String idNumber, final int age, final double grade) throws IllegalUserInputArgumentException { this.firstName = firstName; this.lastName = lastName; this.idNumber = idNumber; this.age = age; this.grade = grade; } /** * * @returns the firstname */ public String getFirstName() { return firstName; } /** * * @returns the lasttname */ public String getLastName() { return lastName; } /** * * @returns the student ID number */ public String getIdNumber() { return idNumber; } /** * * @returns the age */ public int getAge() { return age; } /** * * @returns the grade */ public double getGrade() { return grade; } /** * * @returns the true or false if * student passed or failed */ public boolean getPassOrFail() { return pass; } /** * This setter method sets the firstname * @param firstName */ public void setFirstName(final String firstName) throws IllegalUserInputArgumentException { validateFirstName(firstName); } /** * This setter method sets the lastname * @param lastName */ public void setLastName(final String lastName) throws IllegalUserInputArgumentException { String lastNameValidation = "^[a-zA-Z]*"; if(!lastName.matches(lastNameValidation)) { throw new IllegalUserInputArgumentException("Invalid lastname: " + lastName); } this.lastName = lastName; } /** * This setter method sets the IdNumber * @param idNumber */ public void setIdNumber(final String idNumber) throws IllegalInvalidIdNumberArgumentException { String validateIdNumber = "^A?[0-9]{8}$"; if(!idNumber.matches(validateIdNumber)) { throw new IllegalInvalidIdNumberArgumentException("Invalid student id"); } this.idNumber = idNumber; } /** * This setter method sets the age * @param age */ public void setAge(final int age) throws IllegalAgeArgumentException { if(age <= 0) { throw new IllegalAgeArgumentException("Invalid age: " + age); } this.age = age; } /** * This setter method sets the grade * @param grade */ public void setGrade(final double grade) throws IllegalGradeArgumentException { String gradeValidation = "^[0-9]{2,3}(.)?[0-9]*"; if(grade <= 0) { throw new IllegalGradeArgumentException("Invalid grade: " + grade); } this.grade = grade; } /** * This setter method sets the pass true or false * @param pass */ public void setPass(final boolean pass) { this.pass = pass; } /*****************************************************************************************************************/ /** Creating Auxiliary private Methods */ /*****************************************************************************************************************/ /** * This private method checks if student passed or failed * If pass grade is grater or equal to passMarks returns true * otherwise returns false */ private boolean checkPassOrFail(final double grade) { boolean checkGrade = false; if(grade >= PASSMARKS ) { checkGrade = true; } else { checkGrade = false; } return checkGrade; } /** * This method validate the userInput. If userInput is invalid throws Exception * @param userInput */ private void validateFirstName(String userInput) throws IllegalUserInputArgumentException { String userInputValidationValidation = "^[a-zA-Z]*"; if(!userInput.matches(userInputValidationValidation)) { throw new IllegalUserInputArgumentException("Invalid input " + "( " + userInput + " ) " + " provided"); } this.firstName = userInput; } }
public class Lab8Driver { public static void main(String[] args) { /** * Declaring variables to save the user input data */ String firstName = null; String lastName = null; String idNumber = null; int age = 0; double grade = 0.0; //Run while loop unit proceed is true boolean proceed = true; //Using Scanner to get user input Scanner scanner = new Scanner(System.in); //Creating Student class String type HashMap HashMap students = new HashMap<>(); do { System.out.println("Do you wish to create a Student? (y/n): "); String choice = scanner.next().toLowerCase(); scanner.nextLine(); if(choice.equals("y")) { System.out.println("Enter first name: "); firstName = scanner.nextLine(); System.out.println("Enter last name: "); lastName = scanner.nextLine(); System.out.println("Enter id number: "); idNumber = scanner.nextLine(); System.out.println("Enter age as a whole number: "); age = scanner.nextInt(); System.out.println("Enter grade as a decimal number: "); grade = scanner.nextDouble(); } else { proceed = false; System.out.println("Data entry finished"); } } while(proceed); scanner.close(); System.out.println(firstName + " " +lastName + " " + idNumber + " " + age + " " + grade); } }
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill