Algorithms and OOD (CSC 207 2014S) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions] [GNU Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2013F (Rebelsky)] [CSC 207 2013S (Walker)] [CSC 207 2011S (Weinman)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Summary: In this laboratory, you will extend your knowledge of numeric values in Java.
Primary Classes Used:
GitHub Repository:
https://github.com/Grinnell-CSC207/lab-classes.
Fork and clone the repository.
Read through the code to make sure that you understand what it does.
a. Extend the Fraction class so that it permits
multiplication of two fractions.
b. Write an experiment that allows you to explore the behavior of the new method.
As you may know, we can represent every non-negative rational number as a whole number plus a fractional value no smaller than 0 and strictly less than 1.
a. Write a method of the Fraction class,
fractional, that identifies and returns this fractional
value as a Fraction. Your procedure need only work for
positive numbers. Here are some tests that illustrate what it's
supposed to do.
@Test
public void testFractional()
{
Fraction f = new Fraction(11,3);
assertEquals("2/3", f.fractional().toString());
f = new Fraction(1,2);
assertEquals("1/2", f.fractional().toString());
f = new Fraction (5,2);
assertEquals("1/2", f.fractional().toString());
f = new Fraction(4,2);
assertEquals("0/2", f.fractional().toString());
} // testFractional()
b. Test your procedure and correct any errors.
Write and test a third constructor for the Fraction
class. This constructor should accept a string as a parameter,
“parse” that string, and generate the appropriate fraction.
For example,
public static void
stringConstructorTest()
{
Fraction f = new Fraction("1/4");
assertEquals(1, f.numerator());
assertEquals(4, f.denominator());
f = new Fraction("11/5");
assertEquals(11, f.numerator());
assertEquals(5, f.denominator());
f = new Fraction("120/3");
assertEquals(40.0, f.toReal(), 0.0001);
} // stringConstructorTest()
You can expect that the string will have two positive integers
separated by a slash. You may find it useful to reflect on the
indexOf method of the java.lang.String class
and on various static methods of the java.lang.Integer
class.
Write and test a class, Counter, that generates
objects that can count. Objects in class Counter
should provide two methods: increment, which adds 1
to the counter, and get, which gets the current value
of the counter. Your class needs one zeroary constructor which
initializes the counter to 0.
Make sure to verify that if you create two separate objects in
class Counter, you can change the two objects separately.
a. If you've included a toString method in
Counter, comment it out.
b. What do you expect to happen if we print out a Counter
using instructions like the following?
PrintWriter pen = new PrintWriter(System.out, true);
Counter c1 = new Counter();
Counter c2 = new Counter();
c1.increment();
c1.increment();
c2.increment();
pen.println("c1: " + c1);
pen.println("c2: " + c2);
c. Check your answer experimentally.
d. As you should have discovered, when a class lacks a
toString method, Java chooses a fairly
naive representation for printing objects in that class.
Add an appropriate toString method
(e.g., one that returns the counter surrounded by angle brackets).
Then verify that the lines above work as you expect.
a. Update your Counter class to include a second
constructor that allows the user to specify a starting value.
b. Update your Counter class to include a
reset method that reset the counter to the
starting value.
c. Test both updates to ensure that they work appropriately.
Identify other methods that would be useful to include in the
Counter class and add them.
Identify other methods that would be useful to include in the
Fraction class and add them.
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions] [GNU Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2013F (Rebelsky)] [CSC 207 2013S (Walker)] [CSC 207 2011S (Weinman)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Copyright (c) 2013-14 Samuel A. Rebelsky.

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this
license, visit http://creativecommons.org/licenses/by/3.0/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.