Anatomy of a Java Program Anatomy of a typical Java program: a Java program is a
ID: 3863660 • Letter: A
Question
Anatomy of a Java Program Anatomy of a typical Java program: a Java program is a collection of cooperating, but independent, classes. From here through the rest of the course, many of our programs will have a similar organization that is different from what you 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 remembering a new student, remembering a student's grades, averaging a 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? ........ Once the user enters "Doe," 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. Some programs will be very simple with a single class with a single main method. By the end of the semester our programs will have several classes in them. My goal at the moment is to introduce you to the concept that a program is not a monolithic thing, having a single file and usually a single function (main). Our programs will almost always be a collection of cooperating classes, and we'll segregate certain tasks to certain classes, like separating business logic from user interaction.
Explanation / Answer
Please find the Java Program For Grade Book application :
GradeBook.java :
public class GradeBook{
public static void main(String[] args) {
Menu menu = new Menu();
menu.DisplayMenu();
}
}
Menu.java
import java.util.Scanner;
public class Menu {
private final String STRING_REP = "--------------- ";
Scanner input = new Scanner(System.in);
private int studentYear;
private double studentTest1, studentTest2, studentAssignment1, studentAssignment2;
private String studentName, run, Marks;
Student student;
public Menu() {
run = "";
Marks = "";
}
public void DisplayMenu() {
String menuError = "";
String menuDescription = STRING_REP + " " +
"1 - Create Student " +
"2 - Display Test Scores " +
"3 - Display Assignment Scores " +
"4 - Display Student Average " +
"5 - Display Class Grade " +
"6 - Exit " + STRING_REP + " ";
boolean quit = false;
do {
System.out.print(menuDescription + " Choose option: ");
switch (input.nextInt()) {
case 1: createStudent(); break;
case 2: displayTestScores(); break;
case 3: displayAssignmentScores(); break;
case 4: displayAverage(); break;
case 5: displayClassGrade(); break;
case 6: quit = true; break;
default: menuError = "Invalid Option!!!"; break;
}
System.out.println(menuError);
} while (!quit);
}
public void createStudent() {
while (!run.equalsIgnoreCase("n")) {
Scanner input = new Scanner(System.in);
System.out.print("Student Name: ");
studentName = input.nextLine();
do{
System.out.print("Student Grade (MAX: 12): ");
studentYear = input.nextInt();
if (studentYear > 12) {
System.out.println("Maximum Grade is 12!!!");
}
} while (studentYear > 12);
student = new Student(studentName, studentYear);
System.out.println(student.getStudentDetails());
System.out.print("Would you like to input " +studentName +"'s " + "marks for tests and assignments? (Y/N): ");
Marks = input.next();
if (Marks.equalsIgnoreCase("y")) {
promptTestScores();
student.setTestScores(studentTest1, studentTest2);
promptAssignmentScores();
student.setAssignmentScores(studentAssignment1, studentAssignment2);
Marks = "";
}
System.out.print("Would you like to create another student? (Y/N): ");
run= input.next();
}
}
public void promptTestScores() {
System.out.print("Insert marks for Test 1: ");
studentTest1 = input.nextDouble();
System.out.print("Test 2: ");
studentTest2 = input.nextDouble();
}
public void promptAssignmentScores() {
System.out.print("Insert marks for Assignment 1: ");
studentAssignment1 = input.nextDouble();
System.out.print("Assignment 2: ");
studentAssignment2 = input.nextDouble();
}
public void displayTestScores() {
student.getTestScores();
}
public void displayAssignmentScores() {
student.getAssignmentScores();
}
public void displayAverage() {
System.out.println(student.computeAverage());
}
public void displayClassGrade() {
}
}
Student.java
import java.util.Random;
public class Student {
final String STRING_REP = "---------------";
private String studentName, classGrade;
private int studentNumber, studentYear;
private double studentAverage, classAverage;
private double[] testsScore, assignmentsScore;
public Student(String studentName, int studentYear) {
this.studentName = studentName;
this.studentYear = studentYear;
this.studentNumber = computeRandomNumber();
testsScore = new double[2];
assignmentsScore = new double[2];
}
public String getStudentDetails() {
return (" " + STRING_REP + " Student Details Student Name: " +
studentName + " Grade: " +
studentYear + " Student Number: " +
studentNumber + " " + STRING_REP + " ");
}
public void getTestScores(){
int count = 1;
System.out.println(STRING_REP);
for(double i : testsScore){
System.out.println("Test " + count + ": " + i);
count++;
}
System.out.println(STRING_REP);
}
public void getAssignmentScores() {
int count = 1;
System.out.println(STRING_REP);
for(double i : assignmentsScore){
System.out.println("Assignment " + count + ": " + i);
count++;
}
System.out.println(STRING_REP);
}
public void setTestScores(double test1, double test2) {
testsScore[0] = test1;
testsScore[1] = test2;
}
public void setAssignmentScores(double assignment1, double assignment2) {
assignmentsScore[0] = assignment1;
assignmentsScore[1] = assignment2;
}
public int computeRandomNumber() {
Random randomNumber = new Random();
int rNumber = 100 * randomNumber.nextInt(999);
return rNumber;
}
public String computeAverage() {
double temp1 = 0, temp2 = 0, array1 = 1, array2 = 1;
for (int i =0; i < testsScore.length; i++) {
temp1 += testsScore[i];
array1 += i;
}
for (int i =0; i < assignmentsScore.length; i++) {
temp2 += assignmentsScore[i];
array2 += i;
}
double average = (temp1 + temp2)/(array1 + array2);
return (STRING_REP + " Averages Average of tests: " + (temp1/array1) +
" Average of assignments: " + (temp2/array2) +
" Total Average: " + average + " " + STRING_REP);
}
public void computeGrade() {
}
}
Output:
---------------
1 - Create Student
2 - Display Test Scores
3 - Display Assignment Scores
4 - Display Student Average
5 - Display Class Grade
6 - Exit
---------------
Choose option: 1
Student Name: qwe
Student Grade (MAX: 12): 12
---------------
Student Details
Student Name: qwe
Grade: 12
Student Number: 55700
---------------
Would you like to input qwe's marks for tests and assignments? (Y/N): Y
Insert marks for Test 1: 20
Test 2: 30
Insert marks for Assignment 1: 10
Assignment 2: 10
Would you like to create another student? (Y/N): N
---------------
1 - Create Student
2 - Display Test Scores
3 - Display Assignment Scores
4 - Display Student Average
5 - Display Class Grade
6 - Exit
---------------
Choose option: 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.