Java Console Programs

pages Pages: 4word Words: 890

Question :

Programming Fundamentals COIT 11222

Assessment item 1—Java Console Program

Objectives

This assessment item relates to the unit learning outcomes as in the Unit Profile.

Details

For this assignment, you are required to develop Java Console Programs to demonstrate you can use Java constructs including input/output via a command line and using GUI dialogs, Java primitive and built-in data types, Java defined objects, selection and looping statements, methods, and various other Java commands. Your program must produce the correct results.

You are only allowed to use techniques which have been covered in the first five weeks of the subject and within the assignment literature, you must use a Scanner object for console input and no advanced data structures like arrays will be used.

What to submit for this assignment

The Java source code:

You will be able to complete the assignment in weekly parts in which you will produce five java source files. (More details below)

Week1.java, Week2.java, Week3.java, Week4.java and Week5.java.

Once you have completed all of the programs and you are ready to submit, compress all source files into a single zip file for submission, do not include your report in the zip file. Only submit a zip not a rar file


  •  Ass1.zip 

Also submit a report including, how long it took to create the programs (approximately), any problems encountered and screen shots of the output produced. (Use Alt-PrtScrn to capture just the console window or your dialogs and you can paste them into your Word document) You should test every possibility in the program and annotate your test screen shots.

  • ReportAss1.docx


You will submit your files by the due date using the “Assignment 1 Submission” link on the Moodle unit website in the Assessment Block or in the relevant week.



Assignment specification

This assignment will require you to write small five programs, do not panic! They will be small programs which will cover the first five weekly topics. Usually students were required to write one largish program to demonstrate the topics for the first five weeks. Students get themselves into trouble when the first assignment is due as they have not practiced the basics skills necessary to complete the assignment. With the assignment divided into five programs you can complete each exercise as we cover the weekly topics, do not let yourself fall behind.

General Instructions


Each program must contain a header comment which contains: Your name and student number, the name of the file, the date and a brief description of the purpose of the program:

// Programmer: Eric Gen S01234567

// File: Week1.java

// Date: January 4 2019

// Purpose: COIT11222 assignment one question one T318

// Use println method to print initials using asterisks



All programs will be aligned and indented correctly, and contains relevant comments for declarations and statements. All variables and objects will be declared with a meaningful name and use lowercase camel notation:

String bookingName;


All coding will be contained within a main method except for question five when a method will be created and used.

For this assignment you will not worry about checking numeric ranges or data types.


Refer to a Java reference textbook and the unit material (available on the unit WEB

site) for further information about the Java programming topics required to complete this assignment.


Check the marking guide (last page) to ensure you have completed every task. You need to match all output exactly as the sample screenshots shown below.

Distance and Melbourne students can email questions directly to me, other campus students should seek help from your local tutor, and you can still contact me if it is urgent, I usually respond to emails very promptly.

Good luck --- Bruce McKenzie COIT11222 unit coordinator term 3 2018 b.mckenzie@cqu.edu.au

Question one (week one topic). Writing output to the screen.

Once you have written your first “Hello World” program you will be able to complete question one.

Implementation

Create a class called Week1 (file: Week1.java) and within it a main method.

Use the command System.out.println(""); to print out the first initial of your first and last names as a matrix of asterisks. For example this is my first and last initials printed.

Week1.java

The first line of asterisks is printed with this command: System.out.println("********");

You may need to use some graph paper to plot where you need to print your asterisks.

If you like you could submit a picture. An attempt at Mickey Mouse! Just do your initials as it takes a while to create a picture.


 Mickey Mouse Picture

Question two (week 2 topics) Input of data types and arithmetic expressions

Nemo Reef Tours program

Nemo Reef Tours is company which takes passengers on a tour of the Great Barrier Reef, the tours depart at 8:00 am and returns around 5:00 pm. A smorgasbord lunch is also provided along with the opportunity to snorkel among the exotic fish and coral.

This program will allow staff at Nemo Reef Tours to compute the cost of a group of passengers to take the tour. There is a flat fee of eighty five dollars and fifty cents ($85.50) per person.

This program will prompt for and read in a booking name using a Scanner object. The booking name will be stored in a String object.

The program will then output the booking name in a prompt to read in the number of passengers in the group (as a whole number i.e. an integer).

Finally the program will display the receipt for the booking. For simplicity the bookings will be for the following day.

You need to replicate the output exactly as shown below.

staff at Nemo Reef Tours


Implementation

Create a class called Week2 (file:Week2.java) and within it a main method as per question one. Import the Scanner class i.e.

import java.util.Scanner;

Within your main create two Scanner objects named inText and inNumber. One for reading text and the other for reading the numbers, it does not really matter here to have separate Scanner objects but there will be problems later when reading a series of text and numbers (see text pg 81).

Create a prompt using System.out.print(); To ask the user for the booking name.

Declare a String object bookingName to store the booking name and use your inText Scanner object and the inbuilt method inText.nextLine();

The booking name is now stored in the String object bookingName.

We can now create a prompt using the booking name to ask for the number of passengers in the group.

Hint: you can join variables and strings using the concatenation operator +

"Enter the number of passengers for " + bookingName + " ==> "

Declare an integer variable to store the number of passengers and use your inNumber Scanner object and the inbuilt method inNumber.nextInt(); to read the number of passengers.

Declare a double variable to represent the charge for the booking. The arithmetic expression to calculate the total charge is very simple: charge = number of passengers * charge per person

Note: the charge per person must be stored as a constant (use the final keyword). Finally output a receipt for the tour booking as per the sample above.

The total charge must be displayed to two decimal points use printf and a format string as follows: System.out.printf("$%.2f", charge);

Question three (week three topics) Decision statements

The management of Nemo Reef tours would like to encourage more passengers to take the tour, so it was decided there would be discounts for a larger number of passengers in the booking group. The discounts are as follows:

Charge per person $85.50 (as above). One to two passengers: no discount.

From three to five passengers: 10% discount. From six to ten passengers: 15% discount.

More than ten passengers: 20% discount.

Create a class Week3 (file: Week3.java) and a main method and copy your code from question two into the main method of week three main.

After you have read the relevant details of the booking i.e. name and number of passengers, you will have to create a series of if – else if statements to calculate the discount to calculate the final charge.

Initially calculate the total charge as in week 2 then apply the discount. charge = charge – (charge * discount)

When you have calculated the total charge output a receipt as you have done in week two code.


Note: you must use constants for all the numeric literals in the else if statements.

Your output needs to match exactly the output as shown below.


output 1


output 2

output 3

output 4

Question four (week four topics) Repetition while and for loops

Create a class Week4 (file:Week4.java) to demonstrate the use of a repetition statement.

Implementation

Using your solution to question three and a while or for loop, repeat the entry of booking names and the number of passengers times where N is the largest digit in your student ID, if your largest digit is less than three then let N = 3. Hint: use N = 3 while testing and submit using the correct N value.

will be declared as an integer constant using the final keyword.

You are to print a title before the input of the booking names and number of passengers (see sample output). Note the different line spacing.

Ensure you are using a separate Scanner objects for reading numbers and text (why?).

When all of the booking names and number of passengers have been entered the average of the number of the passengers for each booking and the total of the charges collected will be reported. Please note you do not need to store the data in an advanced structure such as an array. You will need to have an integer variable to add up the number of passengers to calculate the average, and a double variable to add up the charges.

 average passengers per booking calculation

Your average passengers per booking calculation has to produce a floating point result. To get a floating point result you will need to promote one of the operands to a double.

i.e. average = totalPassengers * 1.0 / N

Question five (week five topics) Methods and GUI I/O

Create a class Week5 (file:Week5.java) by using your solution to question four. This question is identical to question four as the program will read in N booking names and number of passengers and calculate the charges for the tour, however we are going to create a method and we will be using GUI dialog boxes for our I/O.

Implementation

Methods

You will create a value returning method which will accept the number of passengers as a parameter. Use the following method header:

private static double calculateCharge(int passengers)

Copy and paste your “if else if” code from question four for calculating the parking fee into the body of our new method calculateCharge. You should also copy the constants for the numeric literals into the method too. Use the return statement to return the charge.

You can now use your method in the main method loop. charge = calculateCharge(passengers); 

GUI I/O

We will revisit the week two lecture topic using JOptionPane for accepting GUI input and outputting information.

First we will output a welcome message using JOptionPane.showMessageDialog (Replace your console print output).

GUI input and outputting information


Next we will replace the Scanner objects by using JOptionPane.showInputDialog.

JOptionPane.showInputDialog.

The showInputDialog method will return the string entered into the dialog text field bookingName = JOptionPane.showInputDialog(null, "Prompt"); Next you will need to prompt for the number of passengers.

We receive input from the dialog as a string, in order to convert strings to an integer we need the Integer wrapper class and the parseInt method (text pg 370).

int anInteger =

Integer.parseInt(JOptionPane.showInputDialog(null, "Prompt"));

After reading in and converting the number of passengers to an integer you can use this value to calculate the charge for the tour using your method:

charge = calculateCharge(passenger);

Output the receipt using JOptionPane.showMessageDialog(null, "text")

JOptionPane.showMessageDialog

To format your output in the text argument in showMessagedialog you can use:

String.format

String.format("Format string",args)

See the example below for using place holders to format strings, integers and doubles.

String.format("%s\n%d\n$%.2f", bookingName, passengers, charge)

%s is for a string.

%d is for an integer.

%.2f is for a floating point number (including double) formatted to two decimal places.

The \n will produce a newline and you will need to add extra text to the format string to match the output above.

When the N bookings have been entered you will output the average number of passengers per booking and the total charges collected both to two decimal places, you can use a similar format string as above.

 average number of passengers per booking





Show More

Answer :

It took me around 4-5 hours to complete these programs excluding the report part and I did not face any issues while working on them.

Screen shots

1. Question 1

Printing Mickey mouse in java

Mickey Mouse

We are just using print statements in this program for printing a Mickey Mouse.


2. Question 2


Doing Passenger booking in Java


Passenger Booking


We are testing input types and arithmetic expressions in this program.



3. Question 3

Testing the program for 2 passengers





Testing the program for 2 passengers

Testing the program for 4 passengers



Testing the program for 4 passengers


Testing the program for 8 passengers



Testing the program for 8 passengers



Testing the program for 15 passengers


Testing the program for 15 passengers



4. Question 4


 program for 9 bookings



 program for 9 bookings 2


 program for 9 bookings 3

We are testing the use of for loop in this program for 9 bookings.

Question 5

Login Message Box

Login Message Box


Input dialog for Booking Name

Input dialog for Booking Name


Input dialog for Number Of Passengers

Input dialog for Number Of Passengers


Final Output Dialog

Final Output Dialog