JAVA Write a program that meets the following requirements: • Create a class for
ID: 3818726 • Letter: J
Question
JAVA
Write a program that meets the following requirements: • Create a class for students. The class must contain each student’s name, (String), ID (int) and status (int.) The status indicates the student’s class standing: 1 for freshman, 2 for sophomore, 3 for junior and 4 for senior. • Create 20 students whose names are Name1, Name2 and so on, to Name20, and who’s IDs and status are assigned randomly. Print out the array of student objects. • Find all juniors and print their names and IDs. NOTE: Use the Math.random() method to create student ID and status.
Explanation / Answer
import java.lang.* ;
import java.util.*;
class Student
{
private String name;
private int id;
private int status;
public Student(String name,int id,int status)//argument constructor
{
this.name = name;
this.id = id;
this.status = status;
}
public String toString()
{
return "Name : "+name +" ID : "+id + " Status : "+status;
}
public int getStatus()// get method for status
{
return status;
}
}
class TestStudent
{
public static void main (String[] args)
{
String name;
int id,status;
Student[] s = new Student[20];
for(int i=0;i<20;i++)
{
name = "name"+i;
status= (int) Math.ceil(Math.random() * 4);//random number between 1 and 4
id = (int) Math.floor(Math.random() * 101+1);//random number between 1 and 100
s[i] = new Student(name,id,status);
}
for(int i=0;i<20;i++)
{
if(s[i].getStatus() == 3)
System.out.println(s[i].toString()+" ");
}
}
}
output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.