Exercise 2 Introduction to Computer Science, IDC Herzliya, Fall 2010 |

__The exercise objective__ is to practice working with some
elementary programming constructs like variables, arithmetic operations, and
expressions. We will also practice using the services of some Java's built-in
classes.

2.0 – Self Practice |

The textbook that we are using in this course comes with several work sheets, designed to help students practice the concepts presented in the book and in the lectures. Worksheet1 includes 57 short exercises dealing with expressions, casting, and string operations. Start by printing the worksheet on paper.

Exercises 1-14 of the worksheet present 14 expressions. For each expression, write on the worksheet the order in which the operators in the given expression will be evaluated. For example:

Expression (given in the worksheet): a
– b – c – d

Order of evaluation (your answer): 1 2 3

Exercises 15-57 of the worksheet present 42 assignment operations of the general form variable = expression. For each assignment operations, put yourself in the shoes of the compiler, evaluate the expression, and write down on paper the value that will be assigned to the variable.

This is a self-practice exercise: there is no need to submit anything.

General Guidelines for the Remainder of this Exercise |

2. When your programs run on the same inputs demonstrated below, they must produce precisely the same outputs. For example, if the demonstrated output is "the value of 5 plus 3 is 8" then that's precisely what your program must print. Outputs like "the value is 8" will cause loss of points.

3. Your programs must adhere to the style conventions described in the course web site (Resources > Links and Books > Programming Style Guidelines).

4. The grading of this exercise will emphasize correctness, readability, and style.

2.1 –Volume Calculation (10 points) |

The volume of a sphere with radius r is 4/3 π r^{3}.
Write a program (Volume)
that prompts the user for a radius and returns the volume of the sphere that
has this radius. Here is an example of the program's execution (the
user’s input is underlined):

Enter
a radius: __5__

The volume of a sphere with radius 5.0 is 523.5987755982989

The program should treat all the relevant values as double. This exercise makes use of Java’s Scanner and Math classes.

2.2 – Euclidean Distance (10 points) |

The Euclidian distance between two points (*x*1,*y*1)
and (*x*2,*y*2) in the plane is defined as (giving the length of the "kav aviri" between the
two points). Write a program (EDistance)
that reads the coordinates of two points and returns the Euclidean distance
between them. Example:

Enter
the coordinates: __10 20 50 60__

The
Euclidian distance between (10,20) and (50,60) is 56

Note that the program truncates the result, displaying it as an integer. This exercise also makes use of Java’s Scanner and Math classes.

2.3 – Number parsing, take 2 (20 points) |

Write a program (NumberParser) that reads a number and figures out how many hundreds, tens, and units the number contains. Here are three sample runs of this program:

Enter
a number: __547__

5
hundreds, 4 tens, and 7 units.

Enter
a number: __35__

0
hundreds, 3 tens, and 5 units.

Enter
a number: __54362__

543
hundreds, 6 tens, and 2 units.

This exercise makes use of Java’s Scanner class. From this program onward, before you start
working on the code, go away from the computer. Take a piece of paper, think about what you
want to do, write down the algorithm and test it ON PAPER. When you think that you have the right
solution, go ahead and implement it in Java.

2.4 – Mid Blaster (20 points) |

Write a program (MidBlaster) that reads a string of characters and prints its value excluding the middle character. Here are three sample runs:

Enter
a string: __grape__

grpe

Enter
a string: __orange__

orage

Enter
a string: __g__

When the length of the string is even, we define the middle character to be the one sitting in location n/2 (the first location is 0), where n is the number of characters in the string. And, if n = 1, the program removes the single character and prints an empty string. This exercise makes use of Java’s Scanner class.

2.5 – Fraction Average (20 points) |

Write a program (FractionAverage) that reads three fractions and prints their average, as a fraction. Here is a sample run:

Enter
three fractions: __1 2 3 4 2 3__

The average of 1/2, 3/4, and 2/3 is 23/36

This exercise makes use of Java’s Scanner class and the supplied Fraction class. Before starting to work on this exercise, you have to get acquainted with the Fraction class. Start by downloading the files Fraction.java and FractionDemo.java to your working directory. Next, compile both classes. Next, take a look at the code of FractionDemo.java and run it. Once you get a feel of how to work with Fraction, start writing your FractionAverage class. Comment: normally, when someone gives you a Java class to work with, you will get a .class file packed in a jar, without the source code. Here we give you the actual source code, since we want you to practice compiling and working with multiple classes.

2.6 – Random fractions (20 points) |

Write a program (RandomFraction) that draws two random numbers *x*
and *y*. Next, the program
constructs two Fraction objects: x/y and y/x.
Next, the program computes the product x/y * y/x and prints the
result. If everything works properly,
the result should be 1. Here is a sample run:

(32667373/756)
times (756/32667373) equals 1

For the fun of it, run this program a few times. This exercise makes use of Java’s Random class and the supplied Fraction class.