Create an application that: Creates 4 students Build upon the solutions of the p
ID: 3845238 • Letter: C
Question
Create an application that:
Creates 4 students
Build upon the solutions of the previous labs (e.g., using app.java and student.java)
You will need a version of student.java that has a working whatsup() method
Creates a group
First, you have to design the new Java class called group
A group has a name
A group has 4 students
Create two methods in group.java
a method that displays the group name and the name of each student in the group
a method that displays the participation in a group. Participation is the sum of all the group members' (students') participation. Participation is an int number that varies from 1 to 10 (1 meaning very little participation, 10 meaning great participation)
participation starts in student using a random number (in a similar fashion of whatsUp()) to return the student's participation. For instance, when the method is called, we may have: student member #1, John, with a participation of 3; student #2, Mary, with a participation of 10; and student #3, Fred, with a participation of 5, which will give a participation of (3 + 10 + 5) = 18.
in app.java
using the group variable (instance, object) in group.java:
display the group name, and total group participation
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.______________);
App.java
public class app
{
public static void main (String[] args)
{
int age;
student st1 = new student ("Zack","Mills",(age=21));
for(int i=0;i<20;i++){ //generate random behaviours
System.out.println(st1.getName()+" "+st1.generateRandomBehaviour());
}
st1.generaReport();
}
}
student.java
class student
{
String firstName;
String lastName;
int age;
int n = 5; //for this program, number of behaviour is chosen as 5
String behaviour[]={"is reading","is coding","is playing games"," is working"," is sleeping",};
int count[]=new int[n];
student (String informedFirstName, String informedLastName, int informedAge)
{
firstName = informedFirstName;
lastName = informedLastName;
age = informedAge;
for(int i=0;i<5;i++)
count[i]=0; //initialize all activities count to 0 first
}
String getName()
{
return firstName +" " + lastName;
}
String generateRandomBehaviour(){
int k = 5; //Here k is number of random behaviors possible
int rand = (int)(Math.random()*k);
count[rand]=count[rand]+1;
return behaviour[rand];
}
void generaReport(){
System.out.println(" Report of "+getName());
for(int i=0;i<n;i++){
System.out.println(getName()+" "+behaviour[i]+" : "+((count[i])/20.0*100.0)+" percent of the time");
}
}
}
Explanation / Answer
below is your code: -
student.java
class student {
String firstName;
String lastName;
int age;
int n = 5; // for this program, number of behaviour is chosen as 5
String behaviour[] = { "is reading", "is coding", "is playing games", " is working", " is sleeping", };
int count[] = new int[n];
int participation;
student(String informedFirstName, String informedLastName, int informedAge) {
firstName = informedFirstName;
lastName = informedLastName;
age = informedAge;
for (int i = 0; i < 5; i++)
count[i] = 0; // initialize all activities count to 0 first
}
String getName() {
return firstName + " " + lastName;
}
String generateRandomBehaviour() {
int k = 5; // Here k is number of random behaviors possible
int rand = (int) (Math.random() * k);
count[rand] = count[rand] + 1;
return behaviour[rand];
}
String whatsUp() {
return generateRandomBehaviour();
}
int getParticipation() {
int k = 10; // here 10 is maximum participation
int rand = (int) (Math.random() * k) + 1;
participation = rand;
return rand;
}
void generaReport() {
System.out.println(" Report of " + getName());
for (int i = 0; i < n; i++) {
System.out.println(
getName() + " " + behaviour[i] + " : " + ((count[i]) / 20.0 * 100.0) + " percent of the time");
}
}
}
app.java
public class app {
public static void main(String[] args) {
int age;
student st1 = new student("Zack", "Mills", (age = 21));
for (int i = 0; i < 20; i++) { // generate random behaviours
System.out.println(st1.getName() + " " + st1.generateRandomBehaviour());
}
st1.generaReport();
//New tasks implemented here
student st2 = new student("Nolly", "Siks", (age = 21));
student st3 = new student("John", "Fish", (age = 21));
student[] students = {st1,st2,st3};
System.out.println();
group g1 = new group("Players", students);
System.out.println("Group Details: ");
g1.display();
System.out.println();
System.out.println("Group Participation details: ");
g1.displayParticipation();
System.out.println();
System.out.println("Third student details: ");
System.out.println("The student is: "+g1.students[2].getName());
System.out.println(g1.students[2].getName()+" "+ g1.students[2].whatsUp());
}
}
group.java
public class group {
String name;
student[] students;
group() {
name= "";
students = new student[4];
}
group(String name,student[] students) {
this.name = name;
this.students = students;
}
public void display() {
System.out.println("Group Name: "+this.name);
System.out.println("Student names which are in group: ");
for (int i = 0; i < students.length; i++) {
System.out.println(students[i].getName());
}
}
public void displayParticipation() {
int parts = 0;
for (int i = 0; i < students.length; i++) {
parts = parts + students[i].getParticipation();
}
System.out.println("Total Participation of the group "+this.name+": "+parts);
}
}
Sample Output: -
Zack Mills is reading
Zack Mills is sleeping
Zack Mills is playing games
Zack Mills is sleeping
Zack Mills is sleeping
Zack Mills is coding
Zack Mills is working
Zack Mills is sleeping
Zack Mills is sleeping
Zack Mills is reading
Zack Mills is working
Zack Mills is reading
Zack Mills is working
Zack Mills is reading
Zack Mills is sleeping
Zack Mills is coding
Zack Mills is sleeping
Zack Mills is playing games
Zack Mills is coding
Zack Mills is reading
Report of Zack Mills
Zack Mills is reading : 25.0 percent of the time
Zack Mills is coding : 15.0 percent of the time
Zack Mills is playing games : 10.0 percent of the time
Zack Mills is working : 15.0 percent of the time
Zack Mills is sleeping : 35.0 percent of the time
Group Details:
Group Name: Players
Student names which are in group:
Zack Mills
Nolly Siks
John Fish
Group Participation details:
Total Participation of the group Players: 11
Third student details:
The student is: John Fish
John Fish is playing games
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.