Lab Exercise for Assignment 1: Input, Output, Variables.

COMP102/112 2022 Tri 1: Lab Exercise 1


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 text output.


The exercises for this lab involve writing programs very similar to the ones shown in lectures.


Download the zip file for lab exercise 1 and extract it to the COMP-102-112-2022T1-Lab1 folder in your home folder. It should contain templates for the exercises program. Read through the exercises below to see what you need to do. In order to see what your programs should do, you can run the demo program (on the ECS lab computers - they should be in the COMP102 folder on your desktop), or watch the videos accessible from this page.


This lab consists of a series of very small programs to check that you understand the key principles. You should write these programs in the lab session while the tutors are there to help. It is just fine to get the tutors or other students to help you when working on an exercise program - that's how you will learn the key ideas in the course. You do not need to do all the exercises - you can stop a series once you have completed a program in the series without help.


You must complete methods that perform a simple calculation. Each method should ask the user for one or two numbers, and print out the answer of the computation.

  • If you are using a computer in a lab, you can run the CalculatorExercise demo to see what your program should do (look in the assig1-demos folder).
  • Or, you can watch the video demonstrating what your CalculatorExercise program should do.

The file contains one method called milesToKilometers that currently does nothing. You should complete the method so that it converts miles to kilometers:
  • Ask the user for the number of miles
  • Compute the number of kilometers (there are 1.609 kilometers in a mile)
  • Print out the number of kilometers eg = 45.783 kilometers

Write similar methods ( triangleArea , gramsToOunces , etc) to perform the following calculations. You may stop once you have written one of the methods by yourself.
  • Ask the user for the base and the height of a triangle, and print out the area (half the product of the base and the height).
  • Ask the user for grams and print out the number of ounces.
  • Ask the user for the radius of a sphere and print out the surface area (4 * radius2 * π) and the volume (4/3 * radius3 * π)
  • Ask the user for the temperature in degrees Kelvin and print out the temperature in Fahrenheit (fahrenheit is kelvin * 9/5 - 459.67)
  • Ask the user for the number of pizzas, and the cost per pizza and print out the total cost (in dollars).