please make it simple, i do not need anything complicated. so far i have... have
ID: 3877426 • Letter: P
Question
please make it simple, i do not need anything complicated. so far i have...
have app.jave and student class.
class student
{
String firstName;
String lastName;
int age;
student (String informedFirstName, String informedLastName, int informedAge)
{
firstName = informedFirstName;
lastName = informedLastName;
age = informedAge;
}
String getName()
{
return firstName +" " + lastName;
}
}
public class app
{
public static void main(String args[])
{
student st1 = new student("John", "Smith", 20);
System.out.println(st1.firstName);
}
}
Contents Using the classes from Lab 1, app and student, create a solution for implementing some kind of random behavior for your student. Suppose the student can be reading, surfing the web, or interacting with other students. You will need to checlk now and then to see what the student is doing. You will be asking the instance of the student class and what it is doing at certain time intervals. You Will Need To: Create a method in the class student that returns what the student is doing. Have this method generate the behavior randomly. It can't return the same thing every time. · Implement a for loop in the application to call for the student behavior 20 times. In a 20 minute period, you are going to check once a minute 20 times. Give a total count for each activity. Calculate and display the percentage, of the twenty behaviors displayed, spent on each activity. No need to work with TIME functions in Java. We are making a simulation as if every count of the for loop is one minute Generate a report in the end summarizing what the student did in class. Example: John is reading John is reading John is interacting with peers John is surfingExplanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
//app.java class
import java.util.Random;
class student {
String firstName;
String lastName;
int age;
/**
* An array to store the different activities a student can do. It is
* defined as static, so that it will be common for all Student objects, the
* method whatsUp() will pick a random activity from this, and return. You
* are free to add more activities if you like.
*/
static String[] activities = { "reading", "surfing the web",
"interacting with peers", "taking quiz", "coding java" };
student(String informedFirstName, String informedLastName, int
informedAge) {
firstName = informedFirstName;
lastName = informedLastName;
age = informedAge;
}
String getName() {
return firstName + " " + lastName;
}
String whatsUp() {
/**
* Calculating a random index value between 0- array length
*/
int randomIndex = new Random().nextInt(activities.length);
/**
* returning the activity at the random array index
*/
return activities[randomIndex];
}
}
public class app {
public static void main(String args[]) {
student st1 = new student("John", "Smith", 20);
/**
* creating an array to hold the count of appearance of each activities.
* it is of the same size as the activities array in student class,
* means it can be used to store the counts of each activities of a
* student.
*/
int[] counts = new int[student.activities.length];
/**
* checking for student's activity 20 times, incrementing the count for
* each activities using their array index
*/
for (int i = 0; i < 20; i++) {
/**
* checking the student's activity
*/
String str = st1.whatsUp();
/**
* getting the array index of student's activity
*/
int index = indexOf(str, student.activities);
/**
* incrementing the count of that activity
*/
counts[index]++;
System.out.println((i + 1) + " - " + st1.firstName + " is " + str);
}
for (int i = 0; i < counts.length; i++) {
/**
* Calculating the percentage of activities which occured atleast once
*/
if (counts[i] != 0) {
/**
* frequency percentage= (number of occurances / total outcomes)*100
*/
float countPercentage = (float) ((float) (counts[i] / 20.0) * 100.0);
/**
* displaying the results
*/
System.out.printf("%s was %s for %.2f %% of the time ",
st1.firstName, student.activities[i], countPercentage);
}
}
}
/**
* a method to return the index of an element in an array will return -1 if
* element not found
*/
static int indexOf(String str, String[] array) {
for (int i = 0; i < array.length; i++) {
if (array[i].equals(str)) {
return i;
}
}
return -1;
}
}
/*OUTPUT*/
1 - John is taking quiz
2 - John is taking quiz
3 - John is coding java
4 - John is coding java
5 - John is taking quiz
6 - John is taking quiz
7 - John is reading
8 - John is coding java
9 - John is surfing the web
10 - John is taking quiz
11 - John is taking quiz
12 - John is interacting with peers
13 - John is coding java
14 - John is interacting with peers
15 - John is coding java
16 - John is taking quiz
17 - John is surfing the web
18 - John is interacting with peers
19 - John is reading
20 - John is taking quiz
John was reading for 10.00 % of the time
John was surfing the web for 10.00 % of the time
John was interacting with peers for 15.00 % of the time
John was taking quiz for 40.00 % of the time
John was coding java for 25.00 % of the time
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.