Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the classes from Lab 1, app and student , create a solution for implementi

ID: 3843291 • Letter: U

Question

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 check now and then to see what the student is doing. You will be asking the instance of the student class 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 for 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 surfing

This is app.java

public class app
{   
public static void main(String args[])
{
int age;
  
student st1 = new student ("Zack","Mills",(age=21));
  
student st2 = new student("Fred","Fonz",(age=44));

student st3 = new student("John","Smith",(age=20));
  
if (( st1.age < st2.age )&&(st1.age < st3.age))
System.out.println("Youngest is: " + st1.getName());

else if (( st1.age > st2.age )&&(st1.age > st3.age))
System.out.println("oldest is: " + st1.getName());
  
if (( st2.age < st1.age )&&(st2.age < st3.age))   
System.out.println("Youngest is: " + st2.getName());
  
else if (( st2.age > st1.age )&&(st2.age > st3.age))   
System.out.println("oldest is: " + st2.getName());
  
if (( st3.age < st1.age )&&(st3.age < st2.age))   
System.out.println("Youngest is: " + st3.getName());
  
else if (( st3.age > st1.age )&&(st3.age > st2.age))   
System.out.println("oldest is: " + st3.getName());
}
}

this is student.java

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;
  
   }
}

Explanation / Answer

Firstly you need to store set of random behaviours in an array. To pick any behaviour from the array randomly you need to generate a random number from 1 to n ( let n be the number of random behaviours stored in array). To do this you can use Math.random(), which return random number between 0&1. Multiply this by n and you will get a random number between 1 and n . Now use this random number as index of the behaviour array to pick up a behaviour.

Now let's generate a report. The report consists of number of times a student did a particular behaviour in class.

For this create another array count with length of n and initialize all values in the array to 0. This array will store how many times a particular behaviour was done. Index i in array count will store number of times behaviour i was done from behavour array.

Below is the complete program:

import java.util.*;
import java.lang.*;
import java.io.*;

class student
{
        String firstName;
    String lastName;
    int age;
    int n = 10; //for this program, number of behaviour is chosen as 10
    String behaviour[]={"is reading","is surfing","is playing"," is drinking water"," is sleeping","is chatting on phone"," is talking","is eating","is working","is yawning"};
        int count[]=new int[n];
      
    student (String informedFirstName, String informedLastName, int informedAge)
    {
        firstName = informedFirstName;
        lastName = informedLastName;
        age = informedAge;
         for(int i=0;i<10;i++)
        count[i]=0;             //initialize all activities count to 0 first
    }
  
    String getName()
    {
        return firstName +" " + lastName;
              
    }
  
  
    String generateRandomBehaviour(){
        int n = 10; //Here n is number of random behaviors possible
      
        int rand = (int)(Math.random()*n); /*Math.random always generates a random number between 0&1. Multiply it with 10 to get a random number between 1 and 10;*/
        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]+" times");
        }
    }
  
}


public class app
{
  
public static void main (String[] args)
{
      int age;
    
      student st1 = new student ("Zack","Mills",(age=21));
    
      student st2 = new student("Fred","Fonz",(age=44));
   
      student st3 = new student("John","Smith",(age=20));
    
      if (( st1.age < st2.age )&&(st1.age < st3.age))
      System.out.println("Youngest is: " + st1.getName());
   
      else if (( st1.age > st2.age )&&(st1.age > st3.age))
      System.out.println("oldest is: " + st1.getName());
      
      if (( st2.age < st1.age )&&(st2.age < st3.age))   
      System.out.println("Youngest is: " + st2.getName());
    
      else if (( st2.age > st1.age )&&(st2.age > st3.age))     
      System.out.println("oldest is: " + st2.getName());
    
      if (( st3.age < st1.age )&&(st3.age < st2.age))   
      System.out.println("Youngest is: " + st3.getName());
    
      else if (( st3.age > st1.age )&&(st3.age > st2.age))     
      System.out.println("oldest is: " + st3.getName());
    
   
      for(int i=0;i<20;i++){        //generate random behaviours
        
        System.out.println(st1.getName()+" "+st1.generateRandomBehaviour());
        
      }
      st1.generaReport();
}
}

Please give a thumbs up if you liked the solution

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote