Thank you in advanced! I will make sure to rate. Code must be in basic Java. I h
ID: 3862816 • Letter: T
Question
Thank you in advanced! I will make sure to rate.
Code must be in basic Java. I have attatched photos of the book problems as well as the 2 templates refrenced. Also I will add the sample output.
Specific Instructions: Do the following problem from Programming Projects at the end of Chapter 9 of the textbook. The exercises and page number are from the 4th edition of textbook for this class. If you do not have this edition, you need to refer to it to make sure you are attempting the correct problem. Chapter 9: Exercises #5, 6, 7, and 8 page 657 You are to implement all four classes described in the four exercises. For the classes that have advanced date requirements for the price of the ticket, the call to the Constructor must include, besides the ticket number, the days in advance the ticket is being purchased. You are to utilize the instructor’s client test program, Project10_EventTickets.java, to test your solution.
Comment your code to include: 1. Completed header comments in the file JavaClassTemplate.java. You will use this file, renamed, for each of the four classes you define for this project. 2. General comments throughout the source code.
JavaClassTemplate.java
******************************************************************************
*/// Imported Packages// -----------------// None *** Replace with Java packages, as appropriate ***
public class JavaClass // *** "Refactor", specifying your class name ***{
// Public Interface// 1. Constructors// A. Default Constructor
// B. Initializing Constructor// C. Copy Constructor// 2. Mutator Methods// 3. Observer Methods// 4. Virtual (late binding) Methods: Extensions of class "YourClass"
// A. equals(Object) public boolean equals(Object object) {
// Replace "JavaClass" with your class name!
if (!(object instanceof JavaClass) ) { // not an object of this class type return false; }
else { // Replace "YourClass" with your class name! JavaClass other = (JavaClass) object; // "true" must be replaced with expression that determines // equality of two class objects. return true; } }
// B. toString()
// Constructs Description "String" of the object
public String toString(){
// Build your class specific "String" object here...String str = "( )";
return str;
}
// Private Class Data Members// 1. Constant "final" Value Declarations
// 2. Variable Declarations}
// End Class Definition: *** Your Class Name ***
*****************************************************************************************************************************************************
Project10_EventTickets.java
*****************************************************************************************************************************************
import java.util.Scanner;
public class Project10_EventTickets
{
/**************************************************************************
* Method: main(String[])
*
* Program Description
* -------------------
* This program tests the implementation of the abstract "Ticket" class
* and its derived, concrete classes "WalkupTicket", "AdvanceTicket", and
* "StudentAdvanceTicket", which is derived from "AdvanceTicket".
*
* Concrete objects are created and references to them are stored in an
* array of "Ticket" reference elements. The objects created are picked
* to test the various advance date requirements for the price of the
* ticket (based on the purchase days before the ticket event).
*
* Once the array is established, it is iterated and the results of each
* object displayed via the overridden class "Object", "toString()" method.
*
* RETurn
* Type
* ------
* void
*
* ------------------------------- Arguments ------------------------------
* Type Name Description
* -------- ------------ ------------------------------------------------
* String[] args Array of "string" argument(s) passed to main()
* when the Console program is invoked by the User.
*
* Invoked Methods
* ---------------
* None
*
**************************************************************************
*/
public static void main(String[] args)
{
// Create ticket objects of the three ticket types
WalkupTicket walkup = new WalkupTicket(1);
AdvanceTicket advance1 = new AdvanceTicket(2, 12);
AdvanceTicket advance2 = new AdvanceTicket(3, 5);
StudentAdvanceTicket student1 = new StudentAdvanceTicket(4, 15);
StudentAdvanceTicket student2 = new StudentAdvanceTicket(5,3);
// Create an array of references to event Tickets, the element references
// to the sub-class ticket objects created above
Ticket[] tickets = { walkup, advance1, advance2, student1, student2 };
// Describe the Project Solution to the User
introduction();
// Now iterate the list, displaying the ticket number and price
for (Ticket ticket : tickets)
System.out.println(ticket);
} // End method: main(String[])
/**************************************************************************
* Method: introduction()
*
* Method Description
* ------------------
* This static method describes the Project solution to the User.
*
* RETurn
* Type
* ------
* void
*
* ------------------------------- Arguments ------------------------------
* None: All required "parameters" defined as "static", at Class scope
*
* Invoked Methods
* ---------------
* None
*
**************************************************************************
*/
private static void introduction()
{
// Construct the first line of the Introduction: The Project #
String line1 = "CS-" + COURSE_NUMBER + ": Project #" + PROJECT_NUMBER +
" Solution";
// Construct the second line of the Introduction: Chapter, Exercise(s)
// and the page number in the text
// Start the second line with the text Chapter number and page number
String line2 = "Chapter " + CHAPTER_NUMBER + ", page " + PAGE_NUMBER +
", Exercise";
// Now add the Exercise number(s)
if (EXERCISE_NUMBERS.length == 1)
{ // Only 1 Exercise number
line2 += (" #" + EXERCISE_NUMBERS[0]);
}
else
{ // Multiple Exercise numbers to display...
line2 += "s "; // Add the 's' to "Exercise"
// Now add the Exercise number(s) to the second line
for (int index = 0; index < EXERCISE_NUMBERS.length - 1; ++index)
line2 += ("#" + EXERCISE_NUMBERS[index] + ", " );
line2 += ("and #" + EXERCISE_NUMBERS[EXERCISE_NUMBERS.length - 1]);
}
// Now determine the field width of the first line so that it is centered
// above the second line
int line1FieldWidth = line1.length() + (CONSOLE_LINE_LENGTH - line1.length()) / 2;
int line2FieldWidth = line2.length() + (CONSOLE_LINE_LENGTH - line2.length() ) / 2;
String line1Format = "%" + line1FieldWidth + 's';
String line2Format = "%" + line2FieldWidth + 's';
// Now display both lines
System.out.printf(line1Format + " " + line2Format + " ", line1, line2);
// Next, format the text "String" to have lines that are approximately
// the length of Line #2
Scanner parser = new Scanner(DESCRIPTION_TEXT);
int lineLength = 0;
String nextLine = "";
while (parser.hasNext() )
{
String token = parser.next();
int tokenLength = token.length();
if (lineLength + tokenLength < (CONSOLE_LINE_LENGTH + LINE_LENGTH_TOLERANCE) )
{
nextLine += (token + " ");
lineLength += (tokenLength + 1);
}
else
{
System.out.println(nextLine);
nextLine = (token + " ");
lineLength = token.length() + 1;
}
}
parser.close();
if (nextLine.length() > 0)
System.out.println(nextLine);
System.out.println(" ");
}
// Class Private "static", constant "final" Values
// Project Description items
// 1. CS Course Number
private static final int COURSE_NUMBER = 210;
// 2. Textbook Project Number, Chapter Number, Page Number, and Exercise Number(s)
private static final int PROJECT_NUMBER = 10;
private static final int CHAPTER_NUMBER = 9;
private static final int PAGE_NUMBER = 643;
private static int[] EXERCISE_NUMBERS = { 5, 6, 7, 8 };
// 3. Description text
private static String DESCRIPTION_TEXT =
"This program tests the implementation of the abstract "Ticket" class " +
"and its derived, concrete classes "WalkupTicket", "AdvanceTicket", and " +
""StudentAdvanceTicket", which is derived from "AdvanceTicket".";
private static final int CONSOLE_LINE_LENGTH = 68;
private static final int LINE_LENGTH_TOLERANCE = 5;
} // End class definition: Project10_EventTickets
***************************************************************************************************************************************************************************
Sample output:
Client, Test Program Results:
CS-210: Project #10 Solution
Chapter 9, page 643, Exercises #5, #6, #7, and #8
This program tests the implementation of the abstract "Ticket" class and its derived, concrete classes "WalkupTicket", "AdvanceTicket", and "StudentAdvanceTicket", which is derived from "AdvanceTicket".
Number: 1, Price: 50.0
Number: 2, Price: 30.0
Number: 3, Price: 40.0
Number: 4, Price: 15.0 (ID required)
Number: 5, Price: 20.0 (ID required)
Explanation / Answer
import java.util.Scanner;
public abstract class Ticket //abstract base class
{
private int number;
public Ticket(int number)
{
this.number = number;
}
public int getNumber()
{
return number;
}
public abstract double getPrice();
public abstract String toString();
}
public class WalkupTicket extends Ticket
{
public WalkupTicket(int number)
{
super(number);
}
public double getPrice()
{
return 50;
}
public String toString()
{
return "Number : "+getNumber() +", Price : "+getPrice();
}
}
public class AdvanceTicket extends Ticket
{
private int days;
public AdvanceTicket(int number)
{
super(number);
}
public AdvanceTicket(int number,int days)
{
super(number);
this.days = days;
}
public double getPrice()
{
if(days>=10)
return 30;
else
return 40;
}
public String toString()
{
return "Number : "+getNumber() +", Price : "+getPrice();
}
}
public class StudentAdvanceTicket extends AdvanceTicket
{
private int days;
public StudentAdvanceTicket(int number,int days)
{
super(number);
this.days = days;
}
public double getPrice()
{
if(days>=10)
return 15;
else
return 20;
}
public String toString()
{
return "Number : "+getNumber() +", Price : "+getPrice() +"(ID required)";
}
}
public class Project10_EventTickets
{
public static void main(String[] args)
{
// Create ticket objects of the three ticket types
WalkupTicket walkup = new WalkupTicket(1);
AdvanceTicket advance1 = new AdvanceTicket(2, 12);
AdvanceTicket advance2 = new AdvanceTicket(3, 5);
StudentAdvanceTicket student1 = new StudentAdvanceTicket(4, 15);
StudentAdvanceTicket student2 = new StudentAdvanceTicket(5,3);
// Create an array of references to event Tickets, the element references
// to the sub-class ticket objects created above
Ticket[] tickets = { walkup, advance1, advance2, student1, student2 };
// Describe the Project Solution to the User
introduction();
// Now iterate the list, displaying the ticket number and price
for (Ticket ticket : tickets)
System.out.println(ticket);
} // End method: main(String[])
private static void introduction()
{
// Construct the first line of the Introduction: The Project #
String line1 = "CS-" + COURSE_NUMBER + ": Project #" + PROJECT_NUMBER +
" Solution";
// Construct the second line of the Introduction: Chapter, Exercise(s)
// and the page number in the text
// Start the second line with the text Chapter number and page number
String line2 = "Chapter " + CHAPTER_NUMBER + ", page " + PAGE_NUMBER +
", Exercise";
// Now add the Exercise number(s)
if (EXERCISE_NUMBERS.length == 1)
{ // Only 1 Exercise number
line2 += (" #" + EXERCISE_NUMBERS[0]);
}
else
{ // Multiple Exercise numbers to display...
line2 += "s "; // Add the 's' to "Exercise"
// Now add the Exercise number(s) to the second line
for (int index = 0; index < EXERCISE_NUMBERS.length - 1; ++index)
line2 += ("#" + EXERCISE_NUMBERS[index] + ", " );
line2 += ("and #" + EXERCISE_NUMBERS[EXERCISE_NUMBERS.length - 1]);
}
// Now determine the field width of the first line so that it is centered
// above the second line
int line1FieldWidth = line1.length() + (CONSOLE_LINE_LENGTH - line1.length()) / 2;
int line2FieldWidth = line2.length() + (CONSOLE_LINE_LENGTH - line2.length() ) / 2;
String line1Format = "%" + line1FieldWidth + 's';
String line2Format = "%" + line2FieldWidth + 's';
// Now display both lines
System.out.printf(line1Format + " " + line2Format + " ", line1, line2);
// Next, format the text "String" to have lines that are approximately
// the length of Line #2
Scanner parser = new Scanner(DESCRIPTION_TEXT);
int lineLength = 0;
String nextLine = "";
while (parser.hasNext() )
{
String token = parser.next();
int tokenLength = token.length();
if (lineLength + tokenLength < (CONSOLE_LINE_LENGTH + LINE_LENGTH_TOLERANCE) )
{
nextLine += (token + " ");
lineLength += (tokenLength + 1);
}
else
{
System.out.println(nextLine);
nextLine = (token + " ");
lineLength = token.length() + 1;
}
}
parser.close();
if (nextLine.length() > 0)
System.out.println(nextLine);
System.out.println(" ");
}
// Class Private "static", constant "final" Values
// Project Description items
// 1. CS Course Number
private static final int COURSE_NUMBER = 210;
// 2. Textbook Project Number, Chapter Number, Page Number, and Exercise Number(s)
private static final int PROJECT_NUMBER = 10;
private static final int CHAPTER_NUMBER = 9;
private static final int PAGE_NUMBER = 643;
private static int[] EXERCISE_NUMBERS = { 5, 6, 7, 8 };
// 3. Description text
private static String DESCRIPTION_TEXT =
"This program tests the implementation of the abstract "Ticket" class " +
"and its derived, concrete classes "WalkupTicket", "AdvanceTicket", and " +
""StudentAdvanceTicket", which is derived from "AdvanceTicket".";
private static final int CONSOLE_LINE_LENGTH = 68;
private static final int LINE_LENGTH_TOLERANCE = 5;
} // End class definition: Project10_EventTickets
Output:
Exercises #5, #6, #7, and #8
This program tests the implementation of the abstract "Ticket" class
and its derived, concrete classes "WalkupTicket", "AdvanceTicket",
and "StudentAdvanceTicket", which is derived from "AdvanceTicket".
Number: 1, Price: 50.0
Number: 2, Price: 30.0
Number: 3, Price: 40.0
Number: 4, Price: 15.0(ID required)
Number: 5, Price: 20.0(ID required)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.