Assignment 1: Input, Output, Variables.

COMP112 2020 Tri 1: Assignment 1

  • Due 19 Mar 10am

Goals

The goal of this assignment is for you to be able to construct programs in Java that
  • use methods to get simple user input
  • use variables for storing values, and expressions for computing values.
  • use methods for output, both text and graphics.

Preparation

Download the zip file for assignment 1 and extract it to an COMP112-2020T1-Assig1 folder in your home folder. It should contain templates for the two Java programs that will be marked. Note this file is different from the zip file for the lab exercises.

Read through the whole assignment to see what you need to do. In order to see what your programs should do, you can run the demo programs (on the ECS lab computers - they should be in the COMP112 folder on your desktop).

To Submit

  • Your shipping calculator program: ShippingCalculator.java
  • Your flag drawer program: FlagDrawer.java
  • Your Reflection.txt file.

Do not rename these files.

Remember to submit all of these files. When you have submitted them, check that you can read the files listed on the submission page, and complete the submission process.

Summary

There are three parts to the assignment:
  • Shipping Calculator:
    right Write a program to calculate the costs of shipping an order.
  • Flag Drawer:
    right Write a program to draw some more complex flags.
  • Reflection:
    right Write up your reflections on this assignment.

Overview

The exercises for this lab involve writing programs very similar to the ones shown in lectures. The assignment requires you to write two Java programs that have the same concepts as the exercises, but involve a little more complication and more design.

Program 1: Calculate Shipping Costs

Write a ShippingCalculator program that calculates the cost of shipping a package.
  • Video demonstration of ShippingCalculator program.
If you ship a package with the Comprehensive Shipping Co, the charge depends on the size of the package, its weight, and the number of zones it is being shipped through.
  • The size charge is $300.00 per cubic meter per zone.
  • The weight charge is $1.50 per kilogram per zone.
  • There is a handling charge of $1.60 for processing any shipment
  • The total charge is the sum of the size charge and the weight charge, multiplied by the zone number, plus the handling charge.
Core version.
The program should start by asking the user for the size, weight, and number of zones. The size will consist of the length, width, and height in cm, and the weight should be in kilograms. It should then calculate and print out the charge per zone for the size, and the charge per zone for the weight. It should then calculate and print out the total charge.

For example, if the package is 30cm x 10cm x 8cm; weighs 1.2 kg, and is being sent 3 zones, the charges would be:
  • size charge per zone: 0.3 x 0.1 x 0.08 x $300.00  = $0.72
  • weight charge per zone: 1.2 x $1.50 = $1.80
  • zones: 3
  • total charge: ($0.72 + $1.80) x 3 + $1.60 = $9.16

There are three constants your program should use:
  • size rate: $300.00 per cubic meter per zone.
  • weight rate: $1.50 per kilogram per zone.
  • handling charge: $1.60.

Example Interaction:
Shipping Calculator Core
Length of package (cm): 40
Width of package (cm): 30
Height of package (cm): 15
Weight of package (kg): 2.3
Number of zones (minimum 1): 4
size charge per zone: $5.40
weight charge per zone: $3.45
zones: 4
Total charge:  $37.00
Completion version.
There is a discount rate for commercial customers shipping multiple packages to the same address. The shipment must consist of one or two groups of packages where all the packages in a group have the same size and weight. First, there is only one handling charge applied to the whole shipment. Secondly, the charge is discounted by an amount that depends on the number of packages:
  • discount = charge before discount x (1.0 - 1.0/ number of packages) / 3.0 .
The effect of this formula is to give no discount with just one package, gradually increasing to a 33% discount with a lot of packages.

The completion version of the program should ask for
  • The number of zones
  • The number of packages in the first group, and their size and weight
  • The number of packages in the second group, and their size and weight
It should then print out the size and weight charges per zone for each group of packages, the total charge before discount, the discount, and the total after discount.

Example interaction:
Shipping Calculator Completion
Number of zones (minimum 1): 3
Number of packages of first size: 25
Length (cm): 10
Width (cm): 10
Height (cm): 10
Weight (kg): 0.8
Number of packages of second size: 40
Length (cm): 29
Width (cm): 16
Height (cm): 3
Weight (kg): 1.2
------------------------
zones: 3
Group 1 size charge per zone: $7.50
Group 1 weight charge per zone: $30.00
Group 2 size charge per zone: $16.70
Group 2 weight charge per zone: $72.00
Total before discount: $380.21
Discount for 65 items: $124.79
Total after discount:  $255.42

Challenge.
Ensure that all the prices are printed out with exactly 2 decimal places (the standard for money).

The formula for the discount gives an unreasonably large discount (17%) for just two packages. Design a better formula, that still gives no discount for one package, a 33% discount for a shipment of many packages, but a more slowly rising discount in between. For example, the discount should not be more than 10% for 4 packages.

Make your program use your better discount formula, but also print out at the beginning a table of the discount fraction for different numbers of packages.

Having lots of packages of the same size makes it easier to pack the packages into a truck or a pallet. But it would be better to allow more different sizes of packages in the shipment, as long there are at least 10 packages in each group of the same size. Extend your program to allow more than two groups of packages, as long as each group has at least 10 packages.

Program 2: Flag Drawer

In this part of the assignment, you will write a FlagDrawer program with methods that draw several flags, ranging from a very simple flag with three stripes, to more complicated flags with a collection of stars.

  • Video demonstration of FlagDrawer program.

Quick Tips
  • Images of the flags required in this assignment are available in the zip file you downloaded.
  • You can find lots of details, including the correct dimensions and colours of flags from http://www.crwflags.com/fotw/flags/
  • The exact colours of the flags may be difficult to match; it is fine to use the standard colours: red, green, blue, orange, etc. (see the Java Documentation on Color to find out what standard colors are available), but you can use more accurate colours if you wish.
  • You may not use the UI.drawImage method!
  • You can run the FlagDrawer demo in the lab to see an example of what your program might do.
  • Details for each flag can be found in the comments above the relevant methods in the code.

Core

Write methods to draw:
  • The flag of Germany
  • The flag of Norway

Details for each flag can be found in the comments above the relevant methods in the code, as well as in the table below. For each flag there is a small image showing how it should look (this what your program must draw), and a larger image with instructions showing how big each part should be to assist you in writing the code. Make sure to test your methods with at least two different sizes.

Germany: The German flag
instructions The German flag

Norway: Flag of Norway
instructions Flag of Norway

Hint: For any Java color, say Color.red, Color.red.darker() will give a darker shade of red and Color.red.brighter() will give a brighter shade of red.

Completion

Write methods for the Pacman flag and the Czech flag

Pacman: Pacman flag
Czech Republic: Czech flag
instructions Czech flag

Hint: You can use fillArc to fill parts of circles.

Challenge

Write methods for the flag of Jamaica and the three stars flag.

You may have to think outside the box to work out how to draw the flag of Jamaica with the methods provided in the UI class.

Jamaica: jamaica.gif

Getting the stars right on the three stars flag is tricky! You may need to use the documentation to find out more of the drawing actions of the UI in order to do it.

To get full marks, you need to define a method to draw a star at some position, with a specified size, and pointing in a specified direction. You can then reuse this method for each of the three stars. The flag height should be 2/3 of the width (The OPPOSITE of the comment in the code!).

Three stars: three-stars.png

Final Part: Reflection

Answer the following questions in the Reflection.txt file, and make sure you submit it.

Almost certainly, when you wrote your code, you had bugs in it which you had to fix. Some of the bugs were probably easy to identify; others probably took more time.
  1. Characterise the kinds of bugs you had to deal with (eg spelling, punctuation, ...)
  2. What techniques for fixing the bugs were most effective for you? Can you think of better approaches that you could use next time?