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 program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements Stack<T> { private Node<T> top; private int numElements = 0; public int size() { return (numElements); } public boolean isEmpty() { return (top == null); } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); return top.info; } public T pop() throws StackException { Node<T> temp; if (isEmpty()) throw new StackException("Stack underflow."); temp = top; top = top.getLink(); return temp.getInfo();...
Java Program. !ONLY USING WHILE LOOPS! (Completed but with errors, wanted to revise could anyone re-create...
Java Program. !ONLY USING WHILE LOOPS! (Completed but with errors, wanted to revise could anyone re-create this?) Write a program to keep track of the number of miles you have driven during an extended vacation and the number of gallons of gasoline you used during this time, recorded at weekly intervals. This vacation will last over several weeks (the precise number of weeks while making the program is unknown to the coder). Ask the user to enter the number of...
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...
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...
Write a menu driven program in Java (OOP) That contain class for Botique. Data members include...
Write a menu driven program in Java (OOP) That contain class for Botique. Data members include code ,colour , size ,Quantity Your class should contains app accessors and mutators method Your class should contain Parametric and non-parametric constructors (use constructor chaining) Your class should have input and display method.
Explain the java class below, how it make: import java.util.*; import java.util.concurrent.TimeUnit; public class RacingGame {...
Explain the java class below, how it make: import java.util.*; import java.util.concurrent.TimeUnit; public class RacingGame {       ArrayList<Driver> player = new ArrayList<Driver>();    CarType car = new CarType("Ferrari","2018",280,90,15);    Formula formula = new Formula(5);// number of laps    long startTime = System.currentTimeMillis();    long currentTime = System.currentTimeMillis();    int totalDis = 0;       public static void main(String[] args)    {        RacingGame formulaRace = new RacingGame();        formulaRace.player.add(new Driver("Saleh",10,3));        formulaRace.formula.setDistance(20);//lap distance        formulaRace.totalDis...
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;...
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")
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT