143s18 lab01 - lab PDF

Title 143s18 lab01 - lab
Course Foundations Of Computing I
Institution University of Washington
Pages 3
File Size 244.9 KB
File Type PDF
Total Downloads 39
Total Views 212

Summary

lab...


Description

Lab Assignment 1, TCSS 143 Spring 2018 OBJECTIVE The objective of this assignment is to give you practice with the basic programming concepts you should know from 142, except using Java as a programming language. The assignment consists of 4 exercise sets and covers the following: ✓ command line ✓ basic syntax ✓ print function ✓ methods ✓ debugging ASSIGNMENT SUBMISSION Find the lab partner assigned to you for today’s lab and start working on the exercise sets in the order they are listed. Once you are done with a set, check with one of the other pairs sitting next to you regarding their progress – help each other. Then, as a pair, present your solutions to the lab instructor. Each student is to have his version of the programs/answers and be capable of presenting them for the pair. The presenter will be chosen at random by the lab instructor. All the exercises other than the last set need to be shown to the lab instructor before leaving the lab for full credit. If your pair does not manage to finish the last set during the lab time, the last exercise set may be finished at home and submitted to the lab instructor per his instructions. Use jGrasp other than for set 1. 1. Running the program from command line (20%) Let's look at how to save the file, compile, and run the program from command line. You will be typing and compiling the following lines of code:

Please follow the steps: ✓ Open Notepad and add the code as above. ✓ Save the file as: MyFirstJavaProgram.java. (note where exactly you are saving it; try to use the H: drive if available) ✓ To open command prompt at any location Shift + right click on the folder where the file is stored then click “Open command window here” ✓ Type 'javac MyFirstJavaProgram.java ' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line and you will see that you have a new file in your folder called MyFirstJavaProgram.class (Assumption: The path variable is set). ✓ Now, type ' java MyFirstJavaProgram ' to run your program. ✓ You will be able to see ' Hello World ' printed on the window. 2. Correcting Syntax Errors Part 1 (25%) From now on, use jGrasp Syntax errors are the easiest to find and correct. The compiler will tell you where it got into trouble, and its best guess as to what you did wrong. Usually the error is on the exact line indicated by the compiler, or the line just before it; however, if the problem is incorrectly nested braces, the actual error may be at the beginning of the nested block. The first few weeks, the syntax of the language will be your biggest problem, but as you go on the course you will become accustomed to the syntax of Java. Until you get to the point, at least you have the compiler to let you know what is wrong. -1-

As you type in the programs, resist the temptation of fixing the mistakes in the code as you type the code it. We are trying to show you the errors that jGrasp will be showing you so that if you encounter them in the future, you will know what they mean. After you type in the code, compile, and start fixing the errors one by one based on what the compiler is telling you. a. Create a new Java program and name it Lab1a.java /*

This is another type of a comment – a multi-line comment Program Name: Lab1a.java Programmer: Your name Date: Write today's date Purpose: To correct syntax errors

*/ public class test { public static void main (String[] args) { string line = " ------------ "; int x = 2.0; // println is used to display output on the screen system.out.println(" A rose by any other name "); System.out.println(" would smell as sweet ) System.out.println(line); System.out.println("Act " + x + " Scene " x); } } b. Create a new Java program and name it Lab1b.java /* Program Name: Lab1b.java Programmer: Write your name here Date: Write today's date Purpose: To correct methods errors */ public Lab1b { public static void (String[] args) // Method calling drawBox();

{

} // New Static Method: To Draw a Box void drawBox() // Method call to draw a triangle drawTriangle() System.out.println(“+-----+”); System.out.println(“| |”); System.out.println(“| |”); System.out.println(“+-----+”); }; // New Static Method: To Draw a Triangle public void drawTriangle(); { System.out.println(“ * “); System.out.println(“ * * “); System.out.println(“ * * * ”); System.out.println(“* * * *”); } }

-2-

3. Correcting Syntax Errors Part 2 (30%) a. Download the program Oops3.java. It contains 9 errors. Correct the errors. The corrected version of the program should produce the following output: x = x = The z =

10.01 and y = 8.0 10.01 and y = 867.5309 value from main is: 867.5309 5

b. Download the program Oops4.java. It contains 7 errors. Correct the errors. The corrected version of the program should produce the following output: a is the smallest! c. Download the program OopsYetAgain.java. The method averageCalculator is to return an average of the first N numbers (1-N inclusive), where N is a parameter to the method. For example, if N is 3, then the method should return 2. There are several mistakes in the code – including logic errors. Find the mistakes and correct them. The corrected version of the program should produce the following output: The The The The The The

average average average average average average

of of of of of of

numbers numbers numbers numbers numbers numbers

1 1 1 1 1 1

to to to to to to

3 is 2.0 6 is 3.5 12 is 6.5 24 is 12.5 48 is 24.5 96 is 48.5

4. String problem solving (25%) Download the program shell called Caeser.java. The intention for the program is to encrypt phrases consisting of letters and whitespaces using Caesar’s cipher. Cease’s cipher is the type of a substitution cipher in which each letter in the plaintext is replaced by a letter from the key. For example, if the first row in the table below represents the alphabet and the second one represents the key, then the phrase such as “The dog dayz are over”, will become “xlidhskdhec devidsziv”. 0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

B F

C G

D H

E I

F J

G K

H L

I M

J N

K O

L P

M Q

N R

O S

P T

Q U

R V

S W

T X

U Y

V Z

W A

X B

Y C

Z

D

A E

In main, create a string variable plaintext, read a word into it, change it to lowercase, and pass it to the method that encrypts it using the alphabet and the key. Think how you can use String methods you learned about to do so. The method should return the encrypted version that is to be printed to the console from main. The algorithm is: for each plaintext character, find the index of that character in alphabet use the index to find the substitution letter in key append the key letter to the cipher variable Only the brave: if you feel brave today, you can try to figure out how to use the Scanner object to read more than one word – you will need a loop for that but since Java is a bit weird in its processing of a line, this is an optional part. ----- THE END -----

-3-...


Similar Free PDFs