How to finish this java?! Exercises #5, 6, 7, and 8 You are to implement all fou
ID: 3859626 • Letter: H
Question
How to finish this java?!
Exercises #5, 6, 7, and 8
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. The output is shown on the next page.
The java file is:
import java.util.Scanner;
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
The output is:
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.