Deliverables app.java , student.java and group.java as requested below. Contents
ID: 3878176 • Letter: D
Question
Deliverables
app.java, student.java and group.java as requested below.
Contents
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
Creates 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.______________);
Suggestions:
Start by creating a class called group with only one attribute, GROUP_NAME
Make it work using the app to display only the group name
Add the students to the group (this is tricky, but that’s what you have to learn with this lab)
See the examples in the lesson: video (Links to an external site.)Links to an external site.
For Calculating Participation:
You will need a new method called participation in student.java
Start by copying the whatsUp() method in student.java to create a participation method in student.java.
Make the necessary adjustments for it to return a number (it can be an int number from 1 to 10).
Then work on a participation method on group.java gathering participation from each student in the group.
In the app.java create students and create a group (from previous labs and assignments), and then display the group participation.
EDIT:
import java.util.Random;
class student{
String firstName = "John";
//constructor
student(String name){
this.firstName = name;
}
//getname method
public String getName() {
return this.firstName;
}
String[] behavior = {"writing","watching video lectures","solving problems",
"taking quiz", "reading", "coding java",
"applying for scholarships", "surfing", "interacting with peers",
};
int[] frequency = new int[9];
public String whatUp() {
//return random behavior
Random rand = new Random();
int number = rand.nextInt(behavior.length);
frequency[number]++;
return behavior[number];
}
public static void main(String args[]) {
student st1 = new student("John");
for(int i = 1; i <= 20; i++) {
System.out.println(st1.getName() + " is " + st1.whatUp());
}
for(int i = 0; i < 9; i++) {
float percent = (float)((float)st1.frequency[i]/(float)20) * 100;
System.out.println(st1.getName() + " was " + st1.behavior[i] + " " + percent + "% times.");
}
}
}
Explanation / Answer
Solution
//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.