You Will Need To: Create a method in the class student that returns what the stu
ID: 3877256 • Letter: Y
Question
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 surfing
---------------------------------------
Suggestions
If you need some help, see below for some suggestions:
My suggestion below is a step-by-step solution. Try to implement each step, one at a time.
Start with one of the examples from lesson 01. The new method whatsUp() is one more method in the student class. The student class should still have everything as before (first name, etc, getName() methods, same constructor).
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.
Change the app. Include a for loopsuch as:
for (int j = 0; j < 20; j++)
{
// here, display what the student is doing using the whatsUp( ) method
// You should know how to do create an instance of a student,
// and display some basic information
// You will use something like st1.whatsUp() in your statement
}
If you made it here and read my example with random numbers above, try to change the whatsUp method so that it returns different behaviors randomly.
Use integer variables to count the number of times each activity occurs.
Use float variables to calculate the percentage.
public class app {
public static void main(String[] args) {
student st1 = new student("John", "Smith", 20);
student st2 = new student("Zack", "Mills", 21);
student st3 = new student("Fred", "Fonz", 44);
for (int j = 0; j < 20; j++)
{
System.out.println(st2.getName() + " is " + st2.whatsUp());
}
}
}
public class student {
String firstName;
String lastName;
int age;
student(String fn, String ln, int a)
{
firstName = fn;
lastName = ln;
age = a;
}
String getName()
{
return firstName + " " + lastName;
}
int getAge()
{
return age;
}
String[] task = {"working on a crazy Java lab.", "reading for fun.", "watching a movie.",
"heading to the library.", "walking to class.", "attending class.", "ordering pizza.",
"making pancakes.", "riding a bike.", "going to work.", "staring out a window.",
"taking a nap.", "falling down stairs.", "taking a picture.", "talking on the phone.",
"meeting up with friends.", "watching a baseball game.", "playing hockey.", "studying for class.",
"skating down the street."};
String whatsUp()
{
double r;
int myNumber;
r = Math.random();
myNumber = (int) (r * 20.0);
return task[myNumber];
}
}
Explanation / Answer
import java.util.HashMap;
public class Student {
String firstName;
String lastName;
int age;
//store counts in map
HashMap<Integer, Integer>taskCount;
Student(String fn, String ln, int a)
{
taskCount = new HashMap<>();
firstName = fn;
lastName = ln;
age = a;
}
//Calculate total count of behaviors
public int getTotalCount(){
Integer totalCount = 0;
for (Integer i:taskCount.keySet()){
totalCount += taskCount.get(i);
}
return totalCount;
}
//print percentage value of each behavior
public void printPercentageCount(){
int totalCount = this.getTotalCount();
System.out.println("The percentage task for "+getName());
//for loop on task
for(Integer i= 0;i<this.task.length;i++){
Integer count = taskCount.get(i);
if( count == null)
count = 0 ;
//calculate percentage
float percentage = (float)count/totalCount;
System.out.println(task[i]+ " "+percentage+" %");
}
}
String getName()
{
return firstName + " " + lastName;
}
int getAge()
{
return age;
}
String[] task = {"working on a crazy Java lab.", "reading for fun.", "watching a movie.",
"heading to the library.", "walking to class.", "attending class.", "ordering pizza.",
"making pancakes.", "riding a bike.", "going to work.", "staring out a window.",
"taking a nap.", "falling down stairs.", "taking a picture.", "talking on the phone.",
"meeting up with friends.", "watching a baseball game.", "playing hockey.", "studying for class.",
"skating down the street."};
String whatsUp()
{
double r;
Integer myNumber;
r = Math.random();
myNumber = (int) (r * 19.0);
//add count according to behaviour
Integer count = taskCount.get(myNumber);
if (count != null)
taskCount.put(myNumber, count+1);
else
taskCount.put(myNumber,new Integer(1));
return task[myNumber];
}
}
public class App {
public static void main(String[] args) {
Student st1 = new Student("John", "Smith", 20);
Student st2 = new Student("Zack", "Mills", 21);
Student st3 = new Student("Fred", "Fonz", 44);
for (int j = 0; j < 20; j++)
{
System.out.println(st2.getName() + " is " + st2.whatsUp());
}
st2.printPercentageCount();
}
}
Sample output:
Zack Mills is heading to the library.
Zack Mills is talking on the phone.
Zack Mills is watching a movie.
Zack Mills is taking a nap.
Zack Mills is ordering pizza.
Zack Mills is attending class.
Zack Mills is staring out a window.
Zack Mills is meeting up with friends.
Zack Mills is riding a bike.
Zack Mills is meeting up with friends.
Zack Mills is watching a movie.
Zack Mills is reading for fun.
Zack Mills is falling down stairs.
Zack Mills is reading for fun.
Zack Mills is playing hockey.
Zack Mills is working on a crazy Java lab.
Zack Mills is meeting up with friends.
Zack Mills is riding a bike.
Zack Mills is ordering pizza.
Zack Mills is going to work.
The percentage task for Zack Mills
working on a crazy Java lab. 0.05 %
reading for fun. 0.1 %
watching a movie. 0.1 %
heading to the library. 0.05 %
walking to class. 0.0 %
attending class. 0.05 %
ordering pizza. 0.1 %
making pancakes. 0.0 %
riding a bike. 0.1 %
going to work. 0.05 %
staring out a window. 0.05 %
taking a nap. 0.05 %
falling down stairs. 0.05 %
taking a picture. 0.0 %
talking on the phone. 0.05 %
meeting up with friends. 0.15 %
watching a baseball game. 0.0 %
playing hockey. 0.05 %
studying for class. 0.0 %
skating down the street. 0.0 %
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.