Create a class named TennisGame that holds data about a single tennis game. The
ID: 3761518 • Letter: C
Question
Create a class named TennisGame that holds data about a single tennis game. The class has six fields: the names of the two players, the integer final scores for the players, and the String values of the final scores. Include a get method for each of the six fields. Also include a set method that accepts two players names, and another set method that accepts the two integer final score values. The integer final score for a player is the number of points the player won during the game; this value should be in the range of 0 through 4. If either of the set method parameters for a score is not in the range of 0 through 4, assign 0 to both scores and assign error to the String scores. If both players score parameters are 4, assign 0 to both scores and error to the String scores. The String score values are set by the method that sets the integer score values. The String final score for each player contains the traditional names for points in tennis: love, 15, 30, 40, or game, respectively, for the values 0 through 4. Create a subclass named DoublesTennisGame that includes two additional fields for the names of the first two players partners. Include get methods for the names. Override the parent class setNames() method to accept the names of all four players. Write an application named DemoTennisGames that instantiates several objects of each of these classes. Demonstrate that all the methods assign correct values. Save the files as TennisGame.java, DoublesTennisGame.java, and DemoTennisGames.java.
Explanation / Answer
run:
Before calling set methd:
Player1: null Integer Final Score: 0String Final Score: null
Player2: null Integer Final Score: 0String Final Score: null
After calling set methd:
Player1: Jack Integer Final Score: 1String Final Score: Fifteen
Player2: Jill Integer Final Score: 2String Final Score: Thirty
BUILD SUCCESSFUL (total time: 0 seconds)
package tennisgame;
// package DemoTennisGames;
public class TennisGame {
static String Name1, Name2; // two player's names
static int fs1, fs2; // integer values of the final scores
static String fs1s, fs2s; // String values of the final scores
static void get()
{
System.out.println(" Player1: " + Name1 + " Integer Final Score: " + fs1 + "String Final Score: " + fs1s);
System.out.println(" Player2: " + Name2 + " Integer Final Score: " + fs2 + "String Final Score: " + fs2s);
} // end func get
static void setName(String nm1, String nm2) {
Name1 = nm1;
Name2 = nm2;
} // end func set name
static void setScores(int sc1, int sc2){
if ( (0<= sc1) && (sc1 <= 4) ) fs1 = sc1;
else { fs1 = 0; fs1s = "error"; } // end else
// set the traditional name 0 = love, 1 = fifteen, 2 = thirty, 3 = forty, 4 = game
switch(fs1) {
case 0:
fs1s = " Love ";
break;
case 1:
fs1s = " Fifteen ";
break;
case 2:
fs1s = " Thirty ";
break;
case 3:
fs1s = " Forty ";
break;
case 4:
fs1s = " Game ";
break;
default:
fs1s = " error ";
break;
} // end switch fs1
if ( (0<= sc2) && (sc2 <= 4) ) { fs2 = sc2; fs2s = Integer.toString(fs2); } // end then of if
else { fs2 = 0; fs1s = "error"; } // end else
switch(fs2) {
case 0:
fs2s = " Love ";
break;
case 1:
fs2s = " Fifteen ";
break;
case 2:
fs2s = " Thirty ";
break;
case 3:
fs2s = " Forty ";
break;
case 4:
fs2s = " Game ";
break;
default:
fs2s = " error ";
break;
} // end switch fs2
if ( ( sc1 == 4) && (sc2 == 4) ) { fs1 = 0; fs2=0; fs1s = fs2s = "error" ;}
}// end setscore
public static void main(String[] args) {
//TennisGame obj1, obj2;
//obj1 = null;
System.out.println(" Before calling set methd: ");
TennisGame.get();
TennisGame.setName("Jack", "Jill");
TennisGame.setScores(1, 2);
System.out.println(" After calling set methd: ");
TennisGame.get();
} // end main
}
// sub class for doubles
class DoublesTennisGame extends TennisGame {
String pt1, pt2; // partner1, partner2
void getD() { // get Doubles
System.out.println(" Name of Partner1: " + pt1);
System.out.println(" Name of Partner2: " + pt2);
} // end get
void setName(String p1, String p2, String p3, String p4){
pt1 = p1;
pt2 = p2;
super.Name1 = p3; // 1 and 3 are partners
super.Name2 = p4; // 2 and 4 are partners
} // end setNames func
}
// the above has all 3 java files in one package for testing purpose
// the below has 3 java files as specified in the question:
//////////////// 1 TennisGame.java starts here //////////////////
package tennisgame;
public class TennisGame {
static String Name1, Name2; // two player's names
static int fs1, fs2; // integer values of the final scores
static String fs1s, fs2s; // String values of the final scores
static void get()
{
System.out.println(" Player1: " + Name1 + " Integer Final Score: " + fs1 + "String Final Score: " + fs1s);
System.out.println(" Player2: " + Name2 + " Integer Final Score: " + fs2 + "String Final Score: " + fs2s);
} // end func get
static void setName(String nm1, String nm2) {
Name1 = nm1;
Name2 = nm2;
} // end func set name
static void setScores(int sc1, int sc2){
if ( (0<= sc1) && (sc1 <= 4) ) fs1 = sc1;
else { fs1 = 0; fs1s = "error"; } // end else
// set the traditional name 0 = love, 1 = fifteen, 2 = thirty, 3 = forty, 4 = game
switch(fs1) {
case 0:
fs1s = " Love ";
break;
case 1:
fs1s = " Fifteen ";
break;
case 2:
fs1s = " Thirty ";
break;
case 3:
fs1s = " Forty ";
break;
case 4:
fs1s = " Game ";
break;
default:
fs1s = " error ";
break;
} // end switch fs1
if ( (0<= sc2) && (sc2 <= 4) ) { fs2 = sc2; fs2s = Integer.toString(fs2); } // end then of if
else { fs2 = 0; fs1s = "error"; } // end else
switch(fs2) {
case 0:
fs2s = " Love ";
break;
case 1:
fs2s = " Fifteen ";
break;
case 2:
fs2s = " Thirty ";
break;
case 3:
fs2s = " Forty ";
break;
case 4:
fs2s = " Game ";
break;
default:
fs2s = " error ";
break;
} // end switch fs2
if ( ( sc1 == 4) && (sc2 == 4) ) { fs1 = 0; fs2=0; fs1s = fs2s = "error" ;}
}// end setscore
public static void main(String[] args) {
//TennisGame obj1, obj2;
//obj1 = null;
System.out.println(" Before calling set methd: ");
TennisGame.get();
TennisGame.setName("Jack", "Jill");
TennisGame.setScores(1, 2);
System.out.println(" After calling set methd: ");
TennisGame.get();
} // end main
} // end package
//////////////// 1 TennisGame.java ends here //////////////////
//////////////// 2 DoublesTennisGame.java /// starts here ///////////////
// sub class for doubles
// package DemoTennisGames;
public class DoublesTennisGame extends TennisGame {
String pt1, pt2; // partner1, partner2
void getD() { // get Doubles
System.out.println(" Name of Partner1: " + pt1);
System.out.println(" Name of Partner2: " + pt2);
} // end get
void setName(String p1, String p2, String p3, String p4){
pt1 = p1;
pt2 = p2;
super.Name1 = p3; // 1 and 3 are partners
super.Name2 = p4; // 2 and 4 are partners
} // end setNames func
public static void main(String[] args) {
DoublesTennisGame obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8, obj9, obj10;
DoublesTennisGame.setName("Jack", "Mary", "Melanie", "Peter");
DoublesTennisGame.getD();
}
//////////////// 2 DoublesTennisGame.java /////// ends here ///////////
// demo begins here
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tennisgame;
/**
*
* @author Admin
*/
public class DemoTennisGame {
TennisGame obj1;
DoublesTennisGame DObj1;
TennisGame obj2;
DoublesTennisGame DObj2;
TennisGame obj3;
DoublesTennisGame DObj3;
TennisGame obj4;
DoublesTennisGame DObj4;
TennisGame obj5;
DoublesTennisGame DObj5;
TennisGame obj6;
DoublesTennisGame DObj6;
TennisGame obj7;
DoublesTennisGame DObj7;
TennisGame obj8;
DoublesTennisGame DObj8;
TennisGame obj9;
DoublesTennisGame DObj9;
TennisGame obj10;
DoublesTennisGame DObj10;
}
// demo ends here
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.