HI, I have a java question. My professor said \"Your code in both parts cannot b
ID: 3558856 • Letter: H
Question
HI, I have a java question. My professor said "Your code in both parts cannot be executed due to lack of main() method." I would love to know how to add the main() method but I don't know where to put it, since the orginal code can run withot the main() method. THANK YOU SO MUCH FOR YOUR HELP. :)
Here is the Java code.
public class TestScoreApp
{
private int score1; //set the field score1
private int score2; //set the field score2
private int score3; //set the field score3
public TestScoreApp(int s1, int s2, int s3) //set the constructor
{
score1 = s1;
score2 = s2;
score3 = s3;
}
//mutator methods
public void setScore1(int s1) //the setScore1 method accepts an argument that is stored in the score1 field.
{
score1 = s1;
}
public void setScore2(int s2) //the setScore2 method accepts an argument that is stored in the score2 field.
{
score2 = s2;
}
public void setScore3(int s3) //the setScore3 method accepts an argument that is stored in the score3 field.
{
score3 = s3;
}
// accessor methods
public int getScore1() // the getScore1 method returns the value stored in the score1 field.
{
return score1;
}
public int getScore2() // the getScore2 method returns the value stored in the score2 field.
{
return score2;
}
public int getScore3() // the getScore3 method returns the value stored in the score3 field.
{
return score3;
}
public double getAverageScore() // the getAverageScore method returns the value of average score.
{
return (score1+ score2+ score3)/3;
}
}
import java.util.Scanner; // Needed for the Scanner Class
public class TestScoreTest
{
public static void main(String[] args)
{
int s1, s2, s3;
String sc;
// Create the Scanner object for keyboard.
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your first score: ");
sc= keyboard.nextLine();
s1=Integer.parseInt(sc);// get and store score1
System.out.print("Enter your second score: ");
sc = keyboard.nextLine();
s2 = Integer.parseInt(sc);// get and store score2
System.out.print("Enter your third score: ");
sc = keyboard.nextLine();
s3 = Integer.parseInt(sc); //get and store score3
TestScoreApp object = new TestScoreApp(s1, s2, s3);//Calculate the average value of three scores
System.out.println("Your average Score is "
+object.getAverageScore());//display the average score
System.exit(0);
}
}
Explanation / Answer
import java.util.Scanner; // Needed for the Scanner Class
public class TestScoreApp
{
private int score1; //set the field score1
private int score2; //set the field score2
private int score3; //set the field score3
public TestScoreApp(int s1, int s2, int s3) //set the constructor
{
score1 = s1;
score2 = s2;
score3 = s3;
}
//mutator methods
public void setScore1(int s1) //the setScore1 method accepts an argument that is stored in the score1 field.
{
score1 = s1;
}
public void setScore2(int s2) //the setScore2 method accepts an argument that is stored in the score2 field.
{
score2 = s2;
}
public void setScore3(int s3) //the setScore3 method accepts an argument that is stored in the score3 field.
{
score3 = s3;
}
// accessor methods
public int getScore1() // the getScore1 method returns the value stored in the score1 field.
{
return score1;
}
public int getScore2() // the getScore2 method returns the value stored in the score2 field.
{
return score2;
}
public int getScore3() // the getScore3 method returns the value stored in the score3 field.
{
return score3;
}
public double getAverageScore() // the getAverageScore method returns the value of average score.
{
return (score1+ score2+ score3)/3;
}
public static void main(String[] args)
{
int s1, s2, s3;
String sc;
// Create the Scanner object for keyboard.
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your first score: ");
sc= keyboard.nextLine();
s1=Integer.parseInt(sc);// get and store score1
System.out.print("Enter your second score: ");
sc = keyboard.nextLine();
s2 = Integer.parseInt(sc);// get and store score2
System.out.print("Enter your third score: ");
sc = keyboard.nextLine();
s3 = Integer.parseInt(sc); //get and store score3
TestScoreApp object = new TestScoreApp(s1, s2, s3);//Calculate the average value of three scores
System.out.println("Your average Score is "
+object.getAverageScore());//display the average score
System.exit(0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.