This program must be in Java. IT MUST use the whatsup method shown below or some
ID: 3845919 • Letter: T
Question
This program must be in Java.
IT MUST use the whatsup method shown below or something very similar:
public String whatsUp() {
Random r = new Random();
int rand = r.nextInt(6);
activity [rand] ++ ;
return action[rand];
It needs to have the following Classes App.java, Student.java and Group.java
You need to Create 4 students
Create a group
First you have to design the new Java class called group
A group has a name
A group has 4 students
Create a method in group.java that
Displays the group name and the name of each student in the group
in app.java
Using the group variable (instance, object) in group.java
Display information about what the third student in the group (retrieving information from the student class variables in the group, not from student variables in the app) is up to now (use the whatsUp() method)
For instance: “John” is “doing Java”
Pay attention, you have to use a the group variable (instance, object) in app.java
Something like System.out.println("the student is " + g1.______________);
Explanation / Answer
Here is the code for the question. Sample output is shown. Please don't forget to rate the answer if it helped. Thank you very much.
Student.java
public class Student {
private String name;
public Student(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
Group.java
public class Group {
private Student students[];//array to hold 4 students
private String groupName;
private int count;
public Group(String name)
{
groupName = name;
students = new Student[4];
count = 0;
}
public void addStudent(Student s)
{
if(count < students.length)
{
students[count] = s;
count++;
}
}
public Student getStudent(int i)
{
return students[i];
}
public void display()
{
System.out.println("Group Name: "+groupName);
for(int i = 0; i < count; ++i)
System.out.println(students[i].getName());
}
}
App.java
import java.util.Random;
public class App {
private static String activities[]={"java program","c program"," calculus"," python", "html"};
private static String whatsUp()
{
Random random = new Random(System.currentTimeMillis());
int idx = random.nextInt(activities.length);
return activities[idx];
}
public static void main(String[] args) {
Student s1 = new Student("Bob");
Student s2 = new Student("John");
Student s3 = new Student("Peter");
Student s4 = new Student("Henry");
Group group = new Group("Super Heroes");
group.addStudent( s1);
group.addStudent( s2);
group.addStudent( s3);
group.addStudent( s4);
group.display();
System.out.println(" The 3rd student "+group.getStudent(2).getName()+ " is doing "+whatsUp());
}
}
output
Group Name: Super Heroes
Bob
John
Peter
Henry
The 3rd student Peter is doing python
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.