1) Create a simple class called Student that has the private data properties nam
ID: 3761986 • Letter: 1
Question
1) Create a simple class called Student that has the private data properties name, user name, and gpa. Provide a single constructor that initializes the 3 data properties to the values passed in. Provide a getter method for both the name and gpa and another (getEmail) that returns “@ksu.edu” concatenated on to the end of the user name. Last, create a toString method that could be used to display the following: John Doe JDoe@ksu.edu GPA: 3.4 **Get the Student class to compile before continuing.
2) Download the started class Lab10. Modify the following…
a) Declare an ArrayList to hold Student Objects. b) Within the loop, add code to create a Student object and add it to the declared arraylist. c) Add a loop that uses the toString method in the Student class to display all objects added to the arraylist AFTER all have been entered in by the user. **Get Lab10.java to compile then run and test your program before continuing.
3) What happens if you give the program bad input – for example, a string for GPA? Or simply press enter for the name or username? a) Add the following error checking to your program to handle the following. Provide a proper error message and loop until valid data is entered for each scenario: - Requires that SOMETHING is entered for the name and username (can’t just press enter) (Hint: name equals “” when user simply presses enter…) - Only allows valid GPA values (between 0.0-4.0 / inclusive) - Forces user to re-enter value if a non-numeric value is entered for GPA. Reminder: Not all error checking requires exception handling! Note: You may assume that there won't be any other errors in the program, but if you want to handle other problems, that's fine!
4) Lastly, after displaying all of the students stored in the arraylist, search the arraylist and display ONLY the names and GPAs of those over 3.0 for the dean’s list. Display should look similar to this: Students on the Dean’s list John Doe 3.2 Bill Smith 3.6 etc.
plz help me
Explanation / Answer
Complete Program:
// File: Student.java
public class Student
{
private String name;
private String userName;
private double gpa;
public Student(String name, String userName, double gpa)
{
this.name = name;
this.userName = userName;
this.gpa = gpa;
}
public String getName()
{
return name;
}
public double getGPA()
{
return gpa;
}
public String getEmail()
{
return userName + "@ksu.edu";
}
public String toString()
{
return name + " " + getEmail() + " GPA: " + gpa;
}
} // end class Student
// File: Lab10.java
import java.util.*;
public class Lab10
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
// YOU DO THIS:
// Declare an ArrayList to hold Student Objects
ArrayList<Student> arraylist = new ArrayList<Student>();
String reply;
do
{
System.out.print("Enter the student's name: ");
String name = s.nextLine();
while(name.equals(""))
{
System.out.print("Enter the student's valid name: ");
name = s.nextLine();
}
System.out.print("Enter the student's user name: ");
String userName = s.nextLine();
while(userName.equals(""))
{
System.out.print("Enter the student's valid user name: ");
userName = s.nextLine();
}
System.out.print("Enter the student's GPA: ");
double gpa = Double.parseDouble(s.nextLine());
while(gpa < 0 || gpa > 4)
{
System.out.print("Enter the student's valid GPA: ");
gpa = Double.parseDouble(s.nextLine());
}
// YOU DO THIS:
// Create a student object and add to the arraylist
Student student = new Student(name, userName, gpa);
arraylist.add(student);
System.out.print("Add another student? ('Y' or 'N'): ");
reply = s.nextLine();
System.out.println();
} while(reply.equalsIgnoreCase("Y"));
// YOU DO THIS:
// Create a loop that uses the toString method in the Student class
// to display all objects added to the arraylist
System.out.println(" Details of all students:");
for(int i = 0; i < arraylist.size(); i++)
{
System.out.println(arraylist.get(i));
}
System.out.println(" Students on the Dean's list");
for(int i = 0; i < arraylist.size(); i++)
{
if(arraylist.get(i).getGPA() > 3.0)
System.out.println(arraylist.get(i).getName() + " " + arraylist.get(i).getGPA());
}
} // end main
} // end class Lab10
Sample Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.