Assignment 1: CMPT 371 - SFU Computing Science

(7 points) Exercise 24 page 95 of your text .... The single assignment2.zip file
containing all three java programs will then be submitted to the submission
server. ..... Input Hours, Minutes, and Seconds; Convert hours to the equivalent
number of minutes; Add to the number of minutes in step 2 the number of minutes
Minutes ...

Part of the document


Assignment 2: CMPT 101/104 Simon Fraser University, School of Computing
Science
Due by January 26, 2004

Please refer to the assignment web page for details of assignment
preparation.



1. (7 points) Exercise 24 page 95 of your text
What is printed by the following program? Suppose the input is
20
15


The correct output will be given below. The commented program that
follows explains how the output is generated. Points were only awarded
for the output, the annotated program is for your information




import java.io.*;
public class Mystery
{
// Make an instance of object BufferdReader called keyboard to read
from
// keyboard
static BufferdReader keyboard =
new BufferedReader( new InputStreamReader(System.in) );
// declare and initialize constants, integer NUM and double X
static final int NUM=10;
static final double X = 20.5;


public static void main (String{} arg) throws IOException
{
int a, b;
double z;
char grade;
a = 25;
System.out.println("a = " + a);
//This line prints a = 25 (there is a space before and after =)
then moves
// to the start of a new line


System.out.print("Enter the first integers: ");
// This line prints out the prompt Enter the first integers:
// There is a space at the end of the prompt


System.out.flush();
// This line makes sure all contents in the io buffer are
printed to the
// console before executing the following line


a = Integer.parseInt(keyboard.readLine());
// keyboard.readLine() reads the line of input containing the
// integer 20 followed by enter as a series of 2 characters 2
and 0
// Integer.parseInt (...) parses or translates the characters 2
and 0
// into the integer value 20
// The integer value 20 is assigned to the integer variable a


System.out.println();
// prints a blank line


System.out.print( "Enter the second integers; ");
// This line prints out the prompt Enter the first integers:


// There is a space at the end of the prompt


System.out.flush();
// This line makes sure all contents in the io buffer are
printed to the
// console before executing the following line


b = Integer.parseInt(keyboard.readLine());
// keyboard.readLine() reads the line of input containing the
// integer 15 followed by enter as a series of 2 characters 1
and 5
// Integer.parseInt (...) parses or translates the characters 1
and 5
// into the integer value 15
// The integer value 15 is assigned to the integer variable b


System.out.println();
// prints a blank line


System.out.println("The numbers you entered are " + a + " and "
+ b);
// prints the text below then moves to a new line
// The numbers you entered are 20 and 15


z = X +2 * a - b;
// 2 * a = 2 * 20 = 40 X + (2*a) -b = 20.5 + 40 - 15 = 45.5


System.out.println("z = " + z);
// prints the text below then moves to a new line
// z = 45.5


grade = 'A';
// assigns the character A to the character variable grade


System.out.println("Your grade is " + grade);
// prints the text below then moves to a new line
// Your grade is A


a = 2 * NUM + (int)z;
// 2 * NUM = 2 * 10 = 20, (int)z = (int)45.5 = 45
// 2*NUM+(int)z = 20 + 45 = 65
// assign the value of integer variable a to be 65


System.out.println("The value of a = " + a);
// prints the text below then moves to a new line
// The value of a = 65
}
}
THE OUTPUT IS


a = 25
Enter the first integers: 20


Enter the second integers; 15


The numbers you entered are 20 and 15
z = 45.5
Your grade is A
The value of a = 65




2. (4 points) Exercise 25 page 95 of your text
What type of input does the following program require, and in what
order must the input be provided?


Import java.io.*
public class Strange
{
static BufferedReader keyboard =
new BufferedReader( new InputStreamReader(System.in));
public static void main(String[] arg) throws IOException
{
int x, y;
String name;
//Read a single line of input starting with integer x
x = Integer.parseInt(keyboard.readLine());


//Read a single line (the next line) of input starting
//with the string name
name = keyboard.readLine();


//Read a single line (the next line) of input starting
//with the integer y
y = Integer.parseInt(keyboard.readLine());
}
}


Three lines of input are required
The value of the integer x followed by return
A character string containing the name followed by return
The value of the integer y followed by return


3. (5 points) What conversions between primitive data types are done
implicitly (automatically) in Java? List the possible implicit
conversions and explain what is common between all types of implicit
conversions. Can you implicitly convert from a double to a float? Why
or why not? Write a Java statement to show how you would convert from
a double to a float explicitly?


Conversions that do not lose accuracy, or widening conversions are
done implicitly in Java. Any type can be implicity converted into any
other type that can represent all its values exactly.
short to int
int to long
short to long
int to float
int to double
short to float
short to double
char to int
char to long
float to double
Conversion from double to float can result in loss of information or
accuracy and is therefore not an implicit conversion.
float x;
double y;
x = (float)y; (declarations not necessary)



4. (5 points) Exercise 7 page 141 of your text
Assume the declarations in exercise 6
String str = "Going to the amusement park";
char ch;
int len;
int position;
What is the output of each of the following statements?
System.out.println(str.substring(0,5));
Going
System.out.println(str.substring(13,22));
amusement
System.out.println(str.toUpperCase());
GOING TO THE AMUSEMENT PARK


System.out.println(str.toLowerCase());
going to the amusement park


System.out.println(str.replace('t','*'));
Going *o *he amusemen* park


5. (4 points) Exercise 8 page 141 of your text
What method is used to create an input dialog box?
showInputDialog( String prompt)
What method is used to create an output dialog box?
showMessageDialog(null, String output, String label, int messageType)
What is the name of the class that contains the methods to create input
and output dialog boxes.
JOptionPane
What is the name of the package that contains the class described in c?
java.swing



BEFORE completing the programming problems below please note
Please follow the software development method for each problem
First, in the hard copy portion of the assignment you must include each of
the following
1) A statement of the problem,
2) An analysis of the problem, defining inputs and outputs, developing a
basic algorithm, and presenting a test plan
3) The final refined algorithm.
4) A listing of the final implemented program, including documentation
You must also submit the xxxxx.java files containing each implemented
program to the submission server. You will have one file for each problem.
These three files must be added to the same assignment2.zip file. The
single assignment2.zip file containing all three java programs will then be
submitted to the submission server. Please be sure that the program you
submit matches the listing in the hard copy of your assignment.

6. (25 points) Programming problem: 1 page 143-144 of your text
Statement of the problem:
Read three lines of input with a specified format (see below) from a file
inFile.txt,
Use each line of input to complete a calculation, and print the input to
and the results of each calculation as a single line of output in an output
file outFile.txt

Analysis of the problem
The three lines of input/output are not logically connected so the input,
algorithm outline and output for each line are summarized below
1) The first line of the input file begins with two integers. The values
of the two integers and the sum of those two integers are printed as
the first line in the output file
2) The second line in the input file begins with a valid character, The
character and the character following it in the Unicode table are
printed as the second line of output in the output file
3) The third line of the input file begins with two integers. The values
of the two
integers and the product of those two integers are printed as the
third line in the output file


Detailed Algorithm
1) Read a string containing the first line of input in the input file
2) Tokenize the string, and parse each token to convert the data read to
two integers
3) Add the integers, print the integers themselves and the sum of the
integers in the specified format
4) Read a string containing the second line of input in the input file
5) Use the string library to extr