Using the classes from Lab 1 (app and student), create a solution for implementi
ID: 3844578 • 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.- Shown below under example.
You will need to:
Create a method in the class student that returns what the student is doing.
This method has to 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 for 20 times.
In a 20 minute period, you are going to check every minute = 20 times.
(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.)
Try to implement each step at a time.
1. Create a method called whatsUp( ) in the student class. This method is going to return what the student is doing in class. At first, make it return always the same thing, a string for instance, “surfing the web." Later I will suggest how to return different things.
2. Change the app: include a for loop such as:
Generate a report in the end summarizing what the student has done in class.
Example:
John is reading
John is reading
…..
John is interacting with peers
John is surfing
---------------------------------------
package lab01;
class Student{
String name;
int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String toString(){
return "Name: "+this.name + " " ;
}
}
public class Lab01 {
public static void FindYoungest(Student s1[]){
int index = -1;
int age = Integer.MAX_VALUE;
for(int i=0;i<s1.length;++i){
if(s1[i].age < age){
age = s1[i].age;
index = i;
}
}
if(index!=-1){
System.out.println("The Youngest is:");
System.out.println(s1[index].toString());
}
}
public static void FindOldest(Student s1[]){
int index = -1;
int age = Integer.MIN_VALUE;
for(int i=0;i<s1.length;++i){
if(s1[i].age > age){
age = s1[i].age;
index = i;
}
}
if(index!=-1){
System.out.println("The Oldest is:");
System.out.println(s1[index].toString());
}
}
public static void main(String[] args) {
Student st1 = new Student("John Smith",20);
Student st2 = new Student("Zack Mills ",40);
Student st3 = new Student("Fred Fonz",30);
Student sArray[] = {st1,st2,st3};
FindYoungest(sArray);
FindOldest(sArray);
}
}
Explanation / Answer
Studen2.java
package venkanna;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Student2
{
public String Student()
{
String[] currentRoom;
String[][] rooms = { { "reading" }, { "surfing the web" },
{ "interacting with other students" },
{ "chating with venkannakoothada" } };
Random rand = new Random();
currentRoom = rooms[rand.nextInt(rooms.length)];
//stem.out.println(Arrays.toString(currentRoom));
return Arrays.toString(currentRoom);
}
public static void main(String[] args)
{
Student2 obj = new Student2();
for (int i = 1; i <= 20; i++)
{
System.out.println("Calling " + i + " time");
String behaviour = obj.Student();
System.out.println("Behaviour is"+behaviour);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.