Anatomy of a typical Java program: A Java program is a collection of cooperating
ID: 3801549 • Letter: A
Question
Anatomy of a typical Java program:
A Java program is a collection of cooperating, but independent, classes.
For the rest this course, many of our programs will have a similar organization that is different from what you may have written previously.
Let's consider a program to do a student gradebook. Java programs are a collection of Classes, and Classes contain methods. The book will cover those details at great length.
What I want to point out is that our program will probably have a GradeBook class that does all the work of a gradebook. It will support adding a new student with their grades, and averaging the student's grades, etc. But what this GradeBook class WILL NOT have is any interaction with the user. It will never display a message like
"What student do you want to enter a grade for?" or
"What is the new student's name?"
The user interaction will be in a second class, let's call it GradeBookTest. This class will do all of the user interaction, like asking the above questions. For example, a user's interaction with GradeBookTest might look something like this:
GradeBookTest User
---------------------------------------------------------------------------------
What do you want to do?
Enter 1 to enter a new student
Enter 2 to enter a student's score
Enter 3 to average a student's scores
1
Enter the new student's name
Doe
New Student "Doe" Created
What do you want to do?
Enter 1 to enter a new student
Enter 2 to enter a student's score
Enter 3 to average a student's scores
........
Once the user enters "Doe," the GradeBookTest will call on the GradeBook class to create a new student named "Doe." GradeBook doesn't know or care who its caller is, or how its caller knew to create a new student named "Doe." It simply does what it's told.
GradeBook is said to be implementing "business logic"; it does the "core stuff" of what a gradebook application should do. The reason we split the user interaction from the business logic is that we often want to use the business logic in multiple contexts.
We might want a text-based application, and our GradeBookTest would be a good start on that. We might want to also use it in a graphical user interface (GUI), and we'll write things like that before the course is over. We might want it in a web page, or on an Android device.
If we keep all user interaction out of GradeBook, we're well on our way to using our GradeBook class, unchanged, in any or all of those contexts. As soon as we put any display or input logic into GradeBook, we've doomed it to be married to that user environment. Period.
The exercises should always be clear about what classes you should write.
Explanation / Answer
package chegg;
import java.util.Scanner;
public class GradeBook {
public static void enterStudent(String n){
// add the name to database
}
public static void enterScore(int n){
// enter the score to database
}
public static int averageScore(int n1, int n2, int n3){
int result = 0;
result = (n1+n2+n3)/3;
return result;
}
}
public class GradeBookTest extends GradeBook{
public static void gradebooktest(){
int n = 0;
System.out.println("What do you want to do?");
System.out.println("Enter 1 to enter a new student");
System.out.println("Enter 2 to enter a student's score");
System.out.println("Enter 3 to average a student's scores");
Scanner s = new Scanner(System.in);
n= s.nextInt();
if( n ==1){
System.out.println("Enter the new student's name");
String name = s.next();
enterStudent(name);
System.out.println("New Student" + name + "created");
gradebooktest();
}
else if(n ==2){
System.out.println("Enter the student's score");
int score = s.nextInt();
enterScore(score);
System.out.println("Student's score added");
gradebooktest();
}
else if(n == 3){
System.out.println("Enter the scores");
int n1 = s.nextInt();
int n2 = s.nextInt();
int n3 = s.nextInt();
int result = averageScore(n1, n2, n3);
System.out.println("Average of the scores is" + result);
gradebooktest();
}
else{
return;
}
}
public static void main(String[] args) {
gradebooktest();
}
}