Lab #1: Write, Run, Print

Total Page:16

File Type:pdf, Size:1020Kb

Lab #1: Write, Run, Print

Einstein's Number

Collaboration Solo! Complete this by yourself. Do not copy a solution from anywhere. Do not give your code to anyone or post it anywhere.

Goals  Use Eclipse to write Java code and run programs.  Fix compile time errors as you type.  Become familiar with running a program with standard input and output.  Solve a problem that could use modulus arithmetic  Apply the algorithmic pattern of Input, Process, Output.

Note: It is assumed you are using a CS Lab (228 GS or 930 GS) or you have installed Eclipse on your own computer and created a shortcut / alias / launcher that looks like

A First Java Program: Welcome (need not be turned in)

• 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 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 :-)

Program to be turned in: Einstein Number It is said that Albert Einstein used to take great delight in baffling friends with this puzzle. 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 one. 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 one or more: 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 three digit number. Assume input is in the range of 100 to 998 where the first and third digits are never the same. No 101 or 252 or 989 for example.  541 % 10 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  Math.abs(a - b) returns the positive difference between a and b even if b is bigger

Recommended publications