|
Exercise 2 Introduction to Computer Science, IDC Herzliya, Fall 2009 |
The exercise objective is to practice working with some elementary programming constructs like variables, arithmetic operations, and expressions. We will also practice using some of the services of some widely-used Java's built-in classes.
|
Important Note on Working with Built-in Classes and APIs |
In some of the self-practice questions in this exercise, as well as in some of the programs that you are asked to write, you have to use some of the services of Java's built-in Math and String and Random classes. In general, when working on code or questions that relate to, or make use of, some Java class, it's a good habit to keep a window open with the class API in front of you, so that you can consult with the documentation from time to time on such matters as method availability, syntax rules, and so on. Here are the links to Java's Math API, String API, and Random API.
|
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 of these expressions, your task is to 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 of these assignment operations, your task is to put yourself in the shoes of the computer, evaluate the expression, and then 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.
|
2.1 – Fractions Arithmetic (20 points) |
Write a program (FractionArithmetic) that inputs two fractions and prints their sum and product in both integral and decimal values. Here is an example of this program's execution (the user's input is underlined; everything else is generated by the program):
Enter
the numerator of the first fraction: 2
Enter the denominator of the first fraction: 5
Enter the numerator of the second fraction: 3
Enter the denominator of the second fraction: 10
The sum of the two fractions is 35/50. In decimal: 0.7
The product of the two fractions is 6/50. In decimal: 0.12
You may assume that both the numerator and denominator that the user enters are positive (positive numbers are greater than 0). Also, if the entered or computed fraction is greater than 1, or if it is not reduced, there is no need to reduce it. For example, a fraction of 8/4 can be printed as is, i.e. as 8/4.
Here is another program's execution example:
Enter
the numerator of the first fraction: 1
Enter the denominator of the first fraction: 2
Enter the numerator of the second fraction: 2
Enter the denominator of the second fraction: 3
The sum of the two fractions is 7/6. In decimal: 1.1666666666666667
The product of the two fractions is 2/6. In decimal: 0.3333333333333333
|
2.2 – Time computation, take 1 (10 points) |
Write a program (TimeCalc1) that reads values representing a time duration in hours, minutes, and seconds, and prints the equivalent number of seconds. Here is a typical run-time example of this program (the user's input is underlined; everything else is generated by the program):
Enter
hours: 2
Enter minutes: 28
Enter seconds: 42
This time duration lasts 8922 seconds.
You may assume that all the entered numbers are positive and integral (no decimal points).
|
2.3 – Time computation, take 2 (20 points) |
Write a program (TimeCalc2) that reads a value representing a time duration in seconds, and prints the equivalent time duration in hours, minutes, and seconds. Here is a run-time example:
Enter
time duration in seconds: 8922
This time duration is equivalent to 2 hours, 28 minutes, and 42 seconds.
Here is another run-time example:
Enter
time duration in second: 2017
This time duration is equivalent to 0 hours, 33 minutes, and 37 seconds.
You may assume that the entered input is positive and integral.
|
2.4 – Upper-Case Initials (25 points) |
Write a program (UpperCaseInitials) that reads a string representing a person's name, and prints the string with the first letters of the person's first and last names capitalized. Here is a typical run-time example of this program:
Enter
a name: barack obama
Barack Obama
Another example:
Enter
a name: Gordon brown
Gordon Brown
Yet another example:
Enter
a name: Angela Merkel
Angela Merkel
You may assume that the inputted string includes one space exactly. In other words, names like "Calev Ben Yefune" or "Madonna" are not allowed. There must be a first name, followed by a space, followed by a last name. While working on this program, consult Java's String API.
|
2.5 – Mutated Letters (25 points) |
Write a program (MutatedLetters) that reads a string of upper-case characters. The program then picks one character at random from the string, and replaces all the occurrences of this character throughout the string with the next Unicode character. Here is a typical run-time example of this program:
Enter
a string: I THINK THEREFORE I EXIST
Mutated: I THINK THFRFFORF I FXIST
(In this example, the letter 'E' was randomly chosen and replaced with the letter 'F'.
Here is another example:
Enter
a name: BARACK OBAMA
Mutated: BBRBCK OBBMB
(In this example, 'A' was randomly chosen and replaced with 'B').
You may assume that (a) the inputted string includes upper-case characters only, and that (b) the inputted string includes at least one character. What happens if the character selected at random is Z or a space? In that case, you also have to replace it with the next Unicode character. What are these next Unicode characters? That's something that you can figure out yourself (or let the program figure it out).
While working on this program, consult Java's Random API and String API.
|
General Guidelines |
Your programs must adhere to the programming style and indentation conventions described in the course web site. The grading will emphasize error-free compilation, indentation, and following our submission guidelines.