CSc 331 - Spring 2018
Lab Exercise #1
Preparation
- Create a 331 directory in your home directory.
- Each week, create a lab1, lab2, etc. directory in 331 to organize your files
(for you, and for electronic submission)
- Typically, each Java class is stored in its own file.
Note that the name of this file MUST match the name of the class.
By Java convention, class names always start with a capital letter.
- To compile a Whatever.java to produce Whatever.class, use
javac Whatever.java
If a class uses another class - and there is no "class" file for it, the compiler
will usually compile that class too.
You might have to explicitly delete previous "incorrect" class files.
- (You can place several classes in the same file
- but only one of them can be declared public.
The javac compiler will still generate a separate class file for each Java class.
This approach is not generally recommended.)
- To execute your program, use
java Whatever
(Notice the absence of the .class suffix!)
Exercise 1:
Copy the files ScanInts.java and ScanStrings.java from the directory
~hohman/331/s18/labs/lab1
.
- Examine the source code. Note the use of
the Scanner class to parse input tokens.
By default token delimiter is whitespace.
- Compile and execute this code.
Exercise 2:
Design and code a Java class called Fraction
.
Fraction objects will have 2 private non-negative int members,
representing the numerator and denominator of the fraction.
Fraction Constructors
- Fraction(int top)
The denominator is 1. Assume that top is not negative.
- Fraction(int top, int bottom)
Assume that top is not negative.
Assume that bottom is positive.
Methods
- String toString()
Returns a string representation of this fraction.
For examples "2/3", "1 3/4" and "2".
- Fraction reduce()
Returns a new fraction representing this fraction reduced to lowest terms.
- boolean isReduced().
Returns true if this fraction is in lowest terms
- Fraction add(Fraction x)
Returns a new fraction representing the sum of x and this fraction.
The denominator of the new fraction is the lowest common denominator of
the 2 fractions being added.
For example, 1/6 + 1/9 is 5/18.
The new fraction is not necessarily in lowest terms.
For example, 1/4 + 1/4 is 2/4.
Exercise 3:
Design and code a class AddFractions that will read and add a list of up to
10 fractions as shown below. I think you will need an array of Fraction's.
Notice that extra whitespace on an input line is ignored.
Note that the Java String class has split and trim functions,
and that the Integer class has the parseInt function
...$ java AddFractions
Let's add a list of fractions (no more than 10).
(Use ctrl/d to indicate the end of the list.)
Fraction 1: 3/8
Fraction 2: 1/12
Fraction 3: 11 / 6
Fraction 4: 2
3/8 + 1/12 + 1 5/6 + 2 = 4 7/24
...$
Submission:
This assignment is due at the beginning of the Tuesday lab class next week
(3:30 p.m.) - and the submit program will enforce this!
- Submit your source code electronically using the command
~hohman/bin/submit 331 lab1
from your lab1 directory.