JAVA PROJECT a) Write a class called Student . This class should have the follow
ID: 3678072 • Letter: J
Question
JAVA PROJECT
a) Write a class called Student.
This class should have the following data members:
Number of Students
First Name
Last Name
Cunyfirst ID
GPA
Venus Login ID
Remember to name them appropriately and use lower camel case. eg. First Name = firstName.
b) Write a constructor for your class that accepts all the values listed above and initialize your object appropriately. Your constructor needs to increment the static value Number of Students each time an instantiation occurred so that you can keep track of the students at any given time.
c) Write a static method that will return the number of students.
d) Write a method called getGPA that will return the student’s GPA.
e) Write a Boolean method called isValidVenusLogin to check that what they entered is actually a valid Venus login. As you recall, the Venus login has to be in this format: lafi1234 (First two characters of your last name, first two characters of your first name and your last 4 digits of your CUNYFirst ID).
Hint: Use the Java String API to achieve this.
f) Write a print method called displayAttributes that will output all non-static attribute values. So, for example, you should have something like this:
System.out.println(“Student Name: ” + First Name + “ “ + Last Name);
System.out.println(“GPA: ” + GPA);
g) Create an array of type Student with n objects inside your main. (Use JOptionPane to ask user to tell you how many Student objects you need to instantiate.)
h) Iterate through the array and use proper constructor to instantiate and populate values for each of your student.
i) Use Math.random() to select from a list of predetermined array values dynamically to select a name for each student. For example, you would have an array called firstNames that has the values {“Bob”, “Mary”, “Peter”} and lastNames = {“Peterson”, “Reich”, “Jackson”} etc.
Randomly select the first name and last names for each student during your instantiation as well as for your GPA, CUNY ID, and Venus Login.
j) Iterate through the array of objects and call the isValidVenusLogin method to display whether each student’s Venus Login is valid using this format:
Student <FirstName + LastName>’s Venus Login is <Valid/Invalid>.
If you did not make a mistake during the instantiation of each Student object, you should be getting all valid Venus login.
k) Iterate through the array of objects and call the displayAttributes Method that you have created in (f) to display each student’s attributes.
l) Call the static method that you have created in c) to get and display the number of students using this format:
The number of students that exists now is <number of students>.
For this assignment, you are required to write comments in your program. You want your comments to convey an idea of what you are trying to do to the reader of your code. Think of it this way—if one of your fellow students were to read your code, how would you explain it?
For example:
public static int sum (int n) {
// integer sum is declared and set to 0
int sum = 0;
// for loop from 1 to n
for (int i = 1; i <= n; i++) {
sum += i;
// i is added to sum
} // end of for loop
return sum; // return int sum and blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah…
}
Please don’t do this.
// for loop from 1 to n … // end of for loop
We already know what for loops do. If one is reading your code, you can assume that he/she already knows how to program.
// integer sum is declared and set to 0
We already know that sum is set to 0 by looking at your code. Do not state the obvious.
Do not comment after every line.
Keep an appropriate amount of whitespace. The program is now cluttered.
The comments did not explain the purpose of your code. Now your program is longer and includes unnecessary reading.
Therefore, keep it to the point.
// Method sum adds a sequence of numbers from 1 to n
public static int sum (int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
// sum is updated by adding the next number in the sequence
}
return sum;
}
This is one example of a more straight-to-the-point commenting. Your code does not have to look exactly like this, however you will want to follow the idea.
Explanation / Answer
Student.java
public class Student{
private static int NumberOfStudents = 0;
private String firstName;
private String lastName;
private String cunyID;
private String gpa;
public static int GetNumbersOfStudents(){
return NumberOfStudents;
}
//public static int getGPA(){
// return gpa;
// }
public Student(String newFirstName, String newLastName, String newCUNYID, String newGPA)
{
firstName= newFirstName;
lastName = newLastName;
cunyID= newCUNYID;
gpa = newGPA;
NumberOfStudents++;
}
public void displayAttributes()
{
System.out.println("First Name:" + firstName);
System.out.println("Last Name:" + lastName);
System.out.println("CunyID:" + cunyID);
System.out.println("GPA:" + gpa);
}
public boolean isValidVenusLogin(){
int newcunyID =Integer.parseInt(cunyID);
if (newcunyID<=0 || newcunyID>=99999999);{
System.out.println("A valid venus login.");
return true;
}
}
}
TestClass.java
import javax.swing.*;
import java.util.*;
public class TestClass {
public static int GenerateRandom(int min, int max){
return(int)Math.floor(Math.random()*max) + min;}
public static void main (String[] args){
int numOfStudents= Integer.parseInt(JOptionPane.showInputDialog("Please enter the amount of students whos information you would like to create:"));
String[] lastNames= new String[]{ "Peterson", "Jackson", "Michaels"} ;
String [] firstNames = new String[]{"Bryan", "Larray", "Susan"};
String[] gpas = new String[] {"1.0", "4.0" , "3.0", "2.0" , "2.3" , " 3.4", "3.9"};
String[] cunyIds = new String[]{"12345678", "12312456", "54675632", "40983218", "98023561", "23078625", "71923456"};
Student[] someStudents = new Student[numOfStudents];
for(int i=0; i<someStudents.length; i++)
{
someStudents[i]=
new Student (firstNames[GenerateRandom(1,firstNames.length) -1],
lastNames[GenerateRandom(1,lastNames.length)-1 ],
cunyIds[GenerateRandom(1,cunyIds.length)-1],
gpas[GenerateRandom(1, gpas.length)-1]);
}
for (int i =0 ; i<someStudents.length; i++)
{
someStudents[i].displayAttributes();
someStudents[i].isValidVenusLogin();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.