Simple Calculator
Lab 2 (CS1101 AY2008/9 Semester 1)
This lab assignment is graded.
Date of release: 16 September 2008, Tuesday, 23:59.
Submission deadline: 1 October 2008, Wednesday, 23:59.
School of Computing, National University of Singapore

0 Introduction

This is your second graded lab. Thanks to the recess week you have two weeks to complete this lab.

This lab requires you to do two exercises. You are advised to review the material covered in chapters 1 through 4 and read Programming Style, Design and Marking Guidelines before attempting this lab assignment. You should not use syntax or constructs not covered in lectures; you shouldn't need to and may even be penalized for doing so (if the objective of the assignment is undermined). Unless a template is given, you should start each program afresh (ie. don't cut and paste and modify) so that you get sufficient practice with writing a complete Java program.

A word of advice: Code incrementally. Don't try to finish all parts of a program then compile it. Write your program in bits and pieces and compile frequently. Try to maintain a compilable program even while you are working on it. Submitting a compilable program that partially works is better than submitting an un-compilable one. This last point especially applies to the programming exams. Seriously, code incrementally.

The following topic has not been covered and hence you should not use them:

Note that:

For more information on CourseMarker, please refer to CS1101X lab page.

For this lab, the number of submissions is set to 12. For subsequent labs, it may be reduced. Only your last submission will be graded.

If you have any questions, you may post your queries in the IVLE forum of your respective lecture group. Do not post your programs (partial or complete) in the forum before the deadline!


1 Exercise 1: Calculator (40%)

1.1 Learning objectives

1.2 Task

Write a program Calculator.java to implement a simple scientific calculator which can perform the specified operations:

The operations can be classified into two categories:

  1. a binary operation in which there is a single operator and two operands; OR
  2. a unary operation in which there is a single operator and a single operand.

Examples of binary operations are the standard (+, -, *, /) operations and examples of the unary operations are the (abs, sin, cos, tan) operations. Binary operators appear in the middle of the two operands (e.g. 5 * 2) and unary operators appear before the operand (e.g. abs -23).

You may assume that all operands are integers in the range [-9999, 9999] (that is, from -9999 to 9999 inclusive). The computed result will be output as a value with minimal decimal places and no trailing zeros (subject to a maximum of two decimal places). The following are examples:

    3 + 2 = 5
    3 / 2 = 1.5
    2 / 3 = 0.67
    101 / 1000 = 0.1

For the output, there should be no trailing 0s at the end. That means if we have an answer of 1.99999, and it is rounded up, the output should display 2 and not 2.00.

The user may perform several computations before he chooses to exit the program by typing a single character 'q' and pressing enter.

1.3 Sample run

Sample run using interactive input (user's input shown in blue; output shown in bold purple):

$ javac Calculator.java
$ java Calculator
23 + 40
23 + 40 = 63
50 - 40
50 - 40 = 10
7 / 2
7 / 2 = 3.5
5 * -7
5 * -7 = -35
abs -3
abs -3 = 3
sin 190
sin 190 = -0.17
q

Another sample run is shown below

$ javac Calculator.java
$ java Calculator
-10 / 4
-10 / 4 = -2.5 
cos 10
cos 10 = 0.98 
tan 20
tan 20 = 0.36 
sin 270
sin 270 = -1 
q

1.4 Submission

Submit your program through CourseMarker.

1.5 Important notes


2 Exercise 2: Code Indenter (60%)

2.1 Learning objectives

2.2 Task

In this exercise, you are to write a code indenter CodeIndenter.java to indent a badly indented but compilable java program.

To keep things simple, the following statements about the input are true:

Your output should be indented in the following manner:

2.3 Sample run

Sample run using interactive input (user's input shown in blue; output shown in bold purple):

$ javac CodeIndenter.java
$ java CodeIndenter
import java.util.*;
public class Cube {
public static void main(String[] args) {
                  Scanner stdIn = new Scanner(System.in); System.out.print("Enter length: "); int length = stdIn.nextInt(); 
            System.out.print("Enter width : "); int width = stdIn.nextInt(); System.out.print("Enter height: "); 
int height = stdIn.nextInt(); int volume = length * width * height; System.out.println("Volume = " + volume);
}
}
//eof
import java.util.*;
public class Cube
{
   public static void main(String[] args)
   {
      Scanner stdIn = new Scanner(System.in);
      System.out.print("Enter length: ");
      int length = stdIn.nextInt();
      System.out.print("Enter width : ");
      int width = stdIn.nextInt();
      System.out.print("Enter height: ");
      int height = stdIn.nextInt();
      int volume = length * width * height;
      System.out.println("Volume = " + volume);
   }
}
//eof

Another sample run is shown below:

$ javac CodeIndenter.java
$ java CodeIndenter
import java.util.*;
public class LatLong
{
public static void main(String[]args)
{
Scanner stdin = new Scanner(System.in);
System.out.print("Enter Latitude: ");
double latitude = stdin.nextDouble();
System.out.print("Enter Longitude: ");
double longitude = stdin.nextDouble();
if(latitude > 1.2427 && latitude < 1.4548 && longitude > 103.6271 && longitude < 103.9993)
{
System.out.println("(" + latitude + ", " + longitude + ") is in Singapore.");
}
else
{
System.out.println("(" + latitude + ", " + longitude + ") is not in Singapore.");
}
}
}
//eof
import java.util.*;
public class LatLong
{
   public static void main(String[]args)
   {
      Scanner stdin = new Scanner(System.in);
      System.out.print("Enter Latitude: ");
      double latitude = stdin.nextDouble();
      System.out.print("Enter Longitude: ");
      double longitude = stdin.nextDouble();
      if(latitude > 1.2427 && latitude < 1.4548 && longitude > 103.6271 && longitude < 103.9993)
      {
         System.out.println("(" + latitude + ", " + longitude + ") is in Singapore.");
      }
      else
      {
         System.out.println("(" + latitude + ", " + longitude + ") is not in Singapore.");
      }
   }
}
//eof

2.4 Submission

Submit your program through CourseMarker.

2.5 Important notes

2.6 For discussion


3 Deadline

The deadline for handing in all programs is 1 October 2008, Wednesday, 23:59. Late submissions will NOT be accepted.




Sun Sep 14 18:28:49 SGT 2008