Lab #1: Write, Run, Print

advertisement
C Sc 227 Project 1: Three Main Methods
Collaboration Solo! Complete all four programs by yourself .
Turnin: Print programs 1, 2, and 3 (Welcome.java not needed), staple them together (or lose points) and turn in
to your section leader or Rick at 10:00am Wednesday, 31-August.
Grading Criteria: See the end of this document. Read it before printing any solution!
In this project you are asked to implement four programs where each program is one Java class with one
main method (one example is given below). The first program is in the context of a tutorial describing
the steps required to create a new project, a new class, and how to run the class as a Java program.
Goals




Use Eclipse to write Java code and run programs.
Fix compile time errors as you type.
Become familiar with running programs with standard input and output.
Solve three problems that follow the algorithmic pattern of Input, Process, Output.
It is assumed you are using a CS Lab (either 228 GS or 930 GS) or you have installed Eclipse on your
own computer and created a shortcut / alias / launcher that looks like this:
Create a new Java project, class, and program before working on the other three
• Click the Eclipse Icon
• Eclipse will start and may offer a Welcome page, which you should close (if it is visible)
• Create a new project by clicking on File > New > Java Project
• Enter a name for your project after Project name:
(First is used here)
• Press the Finish button in the lower right corner
• Create a class with File > New > Class
• After Name: type the name of the class you want to create. For this project, the first class you create
will be called Welcome. After you type in Welcome, click the Finish button at bottom.
• Type in the following code as it is shown below. As you type, you may encounter squiggly red lines
under portions of your code. This is the compiler telling you that you may have an error. For help
about the compile time errors, hover the mouse over the red error mark.
• Once everything is typed in, save the file by going to File > Save. Saving the file compiles the code
into a class file and updates error messages.
• Fix any errors before you go on. Save the file again.
• Select Run > Run As > Java Application
• At the bottom of the screen in the Console box, you should see output that says
Welcome. What is your name?
• Click on the console window to the right of this prompt
• Type in your name (input is green like Chris) and press the enter key
• The dialog should look like this (with a different name unless your name is Chris):
Welcome, what is your name? Chris
Hello Chris. I hope you are well :-)
You do not need to turnin the above program.
1) Grade Point Average
Write a Java program that compute a student’s cumulative grade point average (GPA) for three courses.
Credits range from 0.5 to 15.0. Grades can be 0.0, 1.0, 2.0, 3.0, or 4.0. Test your program by running it
with several different inputs where you know the expected results. Your dialog must look like this:
Credits for course 1: 2.0
Grade for course 1: 2.0
Credits for course 2: 3.0
Grade for course 2: 4.0
Credits for course 3: 3.0
Grade for course 3: 4.0
GPA: 3.5
Notes
 Grades range from 0.0 (an E) to 4.0 (an A)
 GPA is a weighted, not simple, average
 Do not round the answer. You may see 1 to 14 digits to the right of the decimal point after GPA:
After testing your program with several test cases, print it with the Eclipse command File > Print
2) Einstein Number
It is said that Albert Einstein used to take great delight in baffling friends with the puzzle below.
First, write the number 1089 on a piece of paper, fold it, and hand it to a friend for safekeeping. What
you wrote down is not to be read until you have completed your amazing mental feat. Next, ask your
friend to write down any three-digit number, emphasizing that the first and last digits must differ by at
least two. Close your eyes or turn your back while this is being done. Better still, have someone
blindfold you.
After your friend has written down the three-digit number, ask him to reverse it, then subtract the
smaller from the larger. Example: 654 - 456 = 198.
Once this is done, tell your friend to reverse the new number. Example: 198 becomes 891.
Next ask your friend to add the new number and its reverse together. Example: 198 + 891 = 1089.
If all goes as planned, your friend will be amazed. The number you wrote down at the start, 1089, will
always be the same as the end result of this mathematical trick.
You program dialog should look like this when the user enter 541
Enter a 3 digit number where the first and last digits differ by at least two: 541
541 -- original
145 -- reversed
396 -- difference
693 -- reverse of the difference
1089 -- sum of the difference and reverse of the difference
Notes
 You don’t need to error check the 3 digit number. Assume it is and the first and last digit differ
by at least 2. Whereas 485 is no good, 486 or 385 are.
 541 % 100 is 1
 541 / 100 is 5
 Math.max(a, b) returns the larger of a and b
 Math.min(a, b) returns the smaller of a and b
After testing your program with several test cases, print it with the Eclipse command File > Print
3) A Little Cryptography
Write a Java program that hides a message in five words. Use one of the characters in the five input
strings to spell out one new word. Run your program with the following input and make up at least one
other message:
Enter five words: cheap energy can cause problems
Enter five integers: 4 2 1 0 5
Secret message: peace
Notes
The String charAt(int) method returns a character, but with + it is treated as if it were an int.
So instead of concatenating five characters, it could add up to the numeric equivalents used to store the
characters. Consider the following code that outputs 195 (97 for 'a' plus 98 for 'b'):
String aString = "ab";
System.out.println(aString.charAt(1) + aString.charAt(0));
// Output: 195
Placing a string literal up front fixes the problem. This code prints the string Secret message: ad
String aString = "abcde";
System.out.println("Secret message: " + aString.charAt(0) + aString.charAt(3));
So make sure you start your output with the label "Secret message: ". This forces charAt to
return value to become the character you seek.
After testing your program with at least two test cases, print it with the Eclipse command File > Print
Grading Criteria: 100 pts
___ /
___ /
___ /
___ /
___ /
___ /
___ /
___/
4 You stapled the three printed programs together for turnin
5 Code in 1) Grade Point Average printout would compile and attempts to solve Program 1
5 Code in 2) Einstein Number printout would compile and attempts to solve Program 2
5 Code in 3) A Little Cryptography would compile and attempts to solve Program 3
15 Code in 1) Grade Point Average is correct. It always works for valid input.
15 Code in 2) Einstein Number is correct. It always works for valid input.
15 Code in 3) A Little Cryptography is correct. It always works for valid input.
36 Style Design
___ / 6 Each program has your name as a comment
___ / 6 Each program has a one or two description of what the program does
___ / 6 Each program has a one or two description of what the program does
___ / 6 You used meaningful program names (same as file names) for each
___ / 6 You used meaningful program names for all identifiers
___ / 6 Your code is consistently formatted with the Eclipse command: File > Source > Format
Sample Documentation for Program 3) Einstein Number with the required comments:
// Programmer: YOUR NAME
//
// It is said that Albert Einstein used to take great delight in
// baffling friends with a puzzle represented by this program.
//
// This program reads a 3 digit number, does some manipulations
// to that integer, and always arrives at 1089.
//
import java.util.Scanner;
public class EinsteinNumber {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Download