You are on page 1of 6

CISB254 Object Oriented Programming using Java

Semester II 2014/2015
Lab 3: You will learn how to create Java programs that we have seen in the lecture
Instruction:
Submit Question 1 by next weeks lab through Moodle. You should submit a word document that
contains java code and screenshot of your output. Name your .doc as Exercise2.doc or Exercise2.docx
Exercise 1:
1. Create a new project, name it Lab3.
2. Create a new class name Ch3Circle.
3. The code for the class should as follows:
//Chapter 3 Sample Program: Compute Area and Circumference
class Ch3Circle {
public static void main( String[] args ) {
final double PI = 3.14159;
double radius, area, circumference;
radius = 2.35;
//compute the area and circumference
area
= PI * radius * radius;
circumference = 2.0 * PI * radius;
System.out.println("Given Radius: " + radius);
System.out.println("Area: " + area);
System.out.println("Circumference: " + circumference);
}
}

4. Compile, run the class and observe the output. The output should be in the default format
Exercise 2:

1. Create a new class named Ch3Circle2


2. The code for the class should be as follows:
// Chapter 3 Sample Program: Compute Area and Circumference with formatting
import java.text.*;
class Ch3Circle2 {
public static void main( String[] args ) {
final double PI = 3.14159;
double radius, area, circumference;
DecimalFormat df = new DecimalFormat("0.000");
radius = 2.35;
//compute the area and circumference
area
= PI * radius * radius;
circumference = 2.0 * PI * radius;
System.out.println("Given Radius: " + df.format(radius));
System.out.println("Area: " + df.format(area));
System.out.println("Circumference: " + df.format(circumference));
}
}

3. Compile, run and observe the output. The output now are formatted according to the specified
format (3 decimal values)

CISB254 Object Oriented Programming using Java


Semester II 2014/2015
Exercise 3:

1. Create a new class named Ch3Circle3


2. The code for the class should be as follows:
/*

Chapter 3 Sample Program: Compute Area and Circumference


with formatting using standard
output

*/
import java.text.*;
class Ch3Circle3 {
public static void main( String[] args )

final double PI = 3.14159;


final String TAB = "\t";
final String NEWLINE = "\n";
double radius, area, circumference;
DecimalFormat df = new DecimalFormat("0.000");
radius
= 2.35;
//Compute the area and circumference
area
= PI * radius * radius;
circumference = 2.0 * PI * radius;
//Display the results
System.out.println(
"Given Radius: " + TAB + df.format(radius) + NEWLINE +
"Area:
" + TAB + df.format(area)
+ NEWLINE +
"Circumference: " + TAB + df.format(circumference));
}
}

3. Compile, run and observe the output. Now the output is properly aligned.
Note: You may use the escape sequence as well to indicate tab and new line (\t and \n)

Exercise 4:

1. Create a new class named Ch3Circle4


2. The code for the class should be as follows:

CISB254 Object Oriented Programming using Java


Semester II 2014/2015

/*Chapter 3 Sample Program: Compute Area and Circumference


with formatting using standard
input and output
*/
import java.util.*;
import java.text.*;
class Ch3Circle4 {
public static void main( String[] args ) {
final double PI = 3.14159;
final String TAB = "\t";
final String NEWLINE = "\n";
double radius, area, circumference;
Scanner scanner = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.000");
//Get input
System.out.print("Enter radius: ");
radius = scanner.nextDouble();
//Compute area and circumference
area
= PI * radius * radius;
circumference = 2.0 * PI * radius;
//Display the results
System.out.println(
"Given Radius: " + TAB + df.format(radius) + NEWLINE +
"Area:
" + TAB + df.format(area)
+ NEWLINE +
"Circumference: " + TAB + df.format(circumference));
}
}

3. Compile, run and observe the output. This program uses input from user and displays the result
in specified format to the console window.
Exercise 5

1. Create a new class named Ch3PoleHeight


2. The code for the class should be as follows:

CISB254 Object Oriented Programming using Java


Semester II 2014/2015

/*
Chapter 3 Sample Program: Estimate the Pole Height
*/
import java.text.*;
import java.util.*;
class Ch3PoleHeight {
public static void main( String[] args ) {
double
double
double
double
double
double

height;
distance;
alpha;
beta;
alphaRad;
betaRad;

//height of the statue


//distance between points A and B
//angle measured at point A
//angle measured at point B
//angle alpha in radians
//angle beta in radians

Scanner scanner = new Scanner(System.in);


//Get three input values
System.out.print("Angle alpha (in degree):"); //try 24
alpha = scanner.nextDouble();
System.out.print("Angle beta (in degree):");
beta = scanner.nextDouble();

//try 23

System.out.print("Distance between points A and B (ft):"); //try 5000


distance = scanner.nextDouble();
//compute the height of the tower
alphaRad = Math.toRadians(alpha);
betaRad = Math.toRadians(beta);
height = (distance * Math.sin(alphaRad) * Math.sin(betaRad))
/
Math.sqrt(Math.sin(alphaRad + betaRad) *
Math.sin(alphaRad - betaRad));
DecimalFormat df = new DecimalFormat("0.000");
System.out.println("\n\nEstimating the height of the pole"
+ "\n\n"
+ "Angle at point A (deg):
" + df.format(alpha)
+ "\n"
+ "Angle at point B (deg):
" + df.format(beta)
+ "\n"
+ "Distance between A and B (ft): " + df.format(distance)+ "\n"
+ "Estimated height (ft):
" + df.format(height));
}
}

CISB254 Object Oriented Programming using Java


Semester II 2014/2015

3. Compile, run and observe the output. In this exercise, you use the standard Math class to
convert degree to radian.
Note: To use methods in Math class, you dont need to create an object because all methods in
class Math are static.
4. You also use DecimalFormat class to format your output
Exercise 6

1. Create a new class named Ch3FindDayOfWeek


2. The code for the class should be as follows:
/*
Chapter 3 Sample Program: Find the Day of Week of a Given Date
*/
import java.util.*;
import java.text.*;
class Ch3FindDayOfWeek {
public static void main( String[] args ) {
int

year, month, day;

GregorianCalendar cal;
SimpleDateFormat sdf;
Scanner scanner = new Scanner(System.in);
System.out.print("Year (yyyy): ");
year
= scanner.nextInt();
System.out.print("Month (1-12): ");
month
= scanner.nextInt();
System.out.print("Day (1-31): ");
day
= scanner.nextInt();
cal = new GregorianCalendar(year, month-1, day);
sdf = new SimpleDateFormat("EEEE");
System.out.println("");
System.out.println("Day of Week: " + sdf.format(cal.getTime()));
}
}

3. Compile, run and observe the output.


Question 1
Write a program that calculates the total tuition fees a student has to pay based on the number of
subjects a user keys in. The fees depend on the number of subjects the student is currently taking. The
formula is:
Fees = 1200 * noOfSubject

CISB254 Object Oriented Programming using Java


Semester II 2014/2015
Where noOfSubject is the number of subject one student takes.
Format your output in 2 decimal places. You may need to use DecimalFormat class for formatting

You might also like