Question

In: Computer Science

Check errors and revise/update this java code below and update with OOp class; at least make...

  1. Check errors and revise/update this java code below and update with OOp class; at least make two classes, use java library, validate user input, format and create UML/Pseudocode and flowchart for the code.

import java.util.Scanner;

public class TestScore

{

   public static void main(String[] args) {

       String firstName;

       String lastName;

       int numTest;

       int i;

       int score;

       double totalScore;

       double avgScore;

       String grade;

       Scanner input = new Scanner(System.in);

       System.out.println("Enter First Name");

       firstName = input.nextLine();

       System.out.println("Enter Last Name");

       lastName = input.nextLine();

       System.out.println("How many test score do you want to enter: ");

       numTest = input.nextInt();

       while(!input.hasNextDouble())

       {

           numTest = input.nextInt();

           System.out.println("Invalid entry, try again");

       }

       totalScore = 0;

       for (i = 0; i <= numTest - 1; i++) {

           score = input.nextInt();

           totalScore = totalScore + score;

       }

       avgScore = totalScore / numTest;

       if (avgScore >= 90) {

           grade = "A";

       } else {

           if (avgScore >= 80) {

               grade = "B";

           } else {

               if (avgScore >= 75) {

                   grade = "C";

               } else {

                   if (avgScore >= 50) {

                       grade = "D";

                   } else {

                       grade = "F";

                   }

               }

           }

       }

       System.out.println(firstName + " " + lastName + " " + avgScore + " " + grade);

   }

}

Solutions

Expert Solution

Updated code with OOP's classes is given below-

There are 2 classes- First is PersonalInfo and Second is Marks class. And then, we have a class Main, that has main method.

import java.util.Scanner;


class PersonalInfo{
String firstName;
String lastName;
  
public PersonalInfo(){
firstName="";
lastName="";
}
  
public void setFirstName(String fname){
firstName=fname;
}
  
public void setLastName(String lname){
lastName=lname;
}
  
public String getFirstName(){
return firstName;
}
  
public String getLastName(){
return lastName;
}
  
  
}

class Marks{
  
int numTest;
int i;
int score;
double totalScore=0;
double avgScore;
String grade;
  
public void setNumTest(int n){
numTest=n;
}
  
public void setScore(int s){
score=s;
totalScore=totalScore+s;
}
  
public int getNumTest(){
return numTest;
}
  
public int getScore(){
return score;
}
  
  
  
public void calResult(){
avgScore=totalScore/numTest;
if (avgScore >= 90) {

grade = "A";

} else {

if (avgScore >= 80) {

grade = "B";

} else {

if (avgScore >= 75) {

grade = "C";

} else {

if (avgScore >= 50) {

grade = "D";

} else {

grade = "F";

}

}

}

}
  
System.out.println("Your average is: "+avgScore);
System.out.println("Your grade is: "+grade);
}
  
  
}


public class Main

{

public static void main(String[] args) {

String firstName, lastName, grade;
int numTest, i, score;
double totalScore, avgScore;

PersonalInfo p=new PersonalInfo();
Marks m=new Marks();
Scanner input = new Scanner(System.in);

System.out.println("Enter First Name");
firstName = input.nextLine();
p.setFirstName(firstName);

System.out.println("Enter Last Name");
lastName = input.nextLine();
p.setLastName(lastName);

System.out.println("How many test score do you want to enter: ");

numTest = input.nextInt();
  
if(numTest<=0){
while(numTest<=0){
System.out.println("Enter correct number of tests!!!");
numTest = input.nextInt();
}
}
m.setNumTest(numTest);

for (i = 0; i <= numTest - 1; i++) {

System.out.println("Enter the marks for test "+(i+1)+":");
score = input.nextInt();
m.setScore(score);

}

m.calResult();
System.out.println("For Student with firstName: "+p.getFirstName()+" and LastName: "+p.getLastName());


}

}

UML Class Diagram for each class is given below-

1) PersonalInfo class

2) Marks class

3) Main class

Do tell if there is any problem in answer!!! Will take care in future :)


Related Solutions

Java OOP - how do I avoid using getter and setter method in player class for...
Java OOP - how do I avoid using getter and setter method in player class for better privacy? I am told to use regular method instead. but I dont know how Thank you ----------------------------------------------------------------------------------------------------------- package MainPackage; import java.util.ArrayList; public class SoccerTeam { private ArrayList<Player> list; public SoccerTeam(int maxSubmission) { list = new ArrayList<>(maxSubmission); } public boolean addPlayer(String newPlayer) { if(newPlayer == null || newPlayer.isEmpty() == true) { return false; } for(Player player : list) { if(player.getName() == newPlayer) { return...
Java Code. Using Decorator pattern make class BurgerToppings: -Each topping is an extra $1 and the...
Java Code. Using Decorator pattern make class BurgerToppings: -Each topping is an extra $1 and the name provided in the constructor -getTotalPrice: returns total price of burger + toppings -getDetails: returns the burger name + name of topping with the word (extra) Code: public interface Burger{ double C(); String getDetails(); } public class BigMac implements Burger{ private String order; private double price ; public BigMac() { this.order= "Big Mac"; this.price = 4.00; } @Override public double getPrice() { return price;...
Java Code. Using Decorator pattern make class BurgerToppings: -Each topping is an extra $1 and the...
Java Code. Using Decorator pattern make class BurgerToppings: -Each topping is an extra $1 and the name provided in the constructor -getTotalPrice: returns total price of burger + toppings -getDetails: returns the burger name + name of topping with the word (extra) Code: public interface Burger{ double C(); String getDetails(); } public class BigMac implements Burger{ private String order; private double price ; public BigMac() { this.order= "Big Mac"; this.price = 4.00; } @Override public double getPrice() { return price;...
1.The below code has some errors, correct the errors and post the working code. Scanner console...
1.The below code has some errors, correct the errors and post the working code. Scanner console = new Scanner(System.in); System.out.print("Type your name: "); String name = console.nextString(); name = toUpperCase(); System.out.println(name + " has " + name.Length() + " letters"); Sample Ouptut: Type your name: John JOHN has 4 letters    2. Write a code that it reads the user's first and last name (read in the entire line as a single string), then print the last name   followed by...
identify the syntax errors in the following code:             public class Hello {                    &
identify the syntax errors in the following code:             public class Hello {                         private static int main(String [] args) {                                     string Msg=”Hello, Wrld!;                                     Sytem.out.println(msg+ “Ken")
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
(code in java please) 14.20 (Check Protection) Computers are frequently employed in check-writing systems, such as...
(code in java please) 14.20 (Check Protection) Computers are frequently employed in check-writing systems, such as payroll and accounts payable applications. There are many strange stories about weekly paychecks being printed (by mistake) for amounts in excess of $1 million. Incorrect amounts are printed by computerized check-writing systems because of human error or machine failure. Systems designers build controls into their systems to prevent such erroneous checks from being issued. Another serious problem is the intentional alteration of a check...
***The code is provided below*** When trying to compile the code below, I'm receiving three errors....
***The code is provided below*** When trying to compile the code below, I'm receiving three errors. Can I get some assistance on correcting the issues? I removed the code because I thought I corrected my problem. I used #define to get rid of the CRT errors, and included an int at main(). The code compiles but still does not run properly. When entering the insertion prompt for the call details, after entering the phone number, the program just continuously runs,...
Find and correct at least two errors in each of the following segments of code. a)...
Find and correct at least two errors in each of the following segments of code. a) final int ARRAY_SIZE = 5; ARRAY_SIZE = 10; int[] table = new int[ARRAY_SIZE]; for (int x = 0; x <= ARRAY_SIZE; x++) table[x] = 99; b) String[] names = {“Mina” , “George”}; int totalLength = 0; for (int i = 0; i <= names.length() ; i++) totalLength += names[i].length; c) String[] words = “Hello”,“Goodbye”; System.out.println(words.toUpperCase()); d) public class MyClass { private int x; private...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT