Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CSE 142 (190) Lab 1: Java Basics University of Washington, CSE 142 (190) Lab 1: Java Basics University of Washington, CSE 142 (190) Lab 1: Java Basics Except where otherwise noted, the contents of this document are Copyright 2010 Stuart Reges and Marty Stepp. lab document created by Whitaker Brand and Marty Stepp Basic lab instructions You may want to bring your textbook to future labs to look up syntax and examples. Stuck? Confused? Have a question? Ask a TA for help, or look at the book or past lecture slides. We encourage you to talk to your classmates for help. You probably won't finish all of the exercises. Do as much as you can within the allotted time. You don't need to keep working on these exercises after you leave the lab. Before you leave today, make sure to check in with one of the TAs in the lab to get credit for your work. Today's lab Goals for today: practice writing, compiling, and running basic Java programs with println statements learn about the jGRASP editor software gain familiarity with syntax errors and debugging learn about various useful CSE 142 online tools and resources practice submitting a program through the course turnin system Exercise 1: Compile and run a program in jGRASP Recall from lecture: A Java program must be compiled, or translated into binary instructions. Then it can be executed or run. When you run a program, it displays output messages to the user in a text window called a console. For our first exercise, let's compile and run a short program that we will provide to you. (See the following slides.) If you get stuck, ask a classmate or TA for help. Exercise 1 - run jGRASP and create file Run the jGRASP editor from the Start Menu of the lab computer (or run it from your own laptop, if you brought one). In jGRASP, create a new Java program: Click File → New → Java from the top menu. An empty white text window should appear in jGRASP. continued on next slide... Exercise 1 - copy/paste Copy and paste the following program into jGRASP: public class MyFirstProgram { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This is my very first program"); System.out.println("in CSE 142. Hooray!"); } } continued on next slide... Exercise 1 - save Save the program: Click the Save toolbar button, or click File → Save from the top menu, or press the hotkey Ctrl-S (Command-S on a Mac). The file name must end with .java and must match the 'public class' name from the Java program. Name this file MyFirstProgram.java. continued on next slide... Exercise 1 - compile Compile the program (translate it to binary instructions): Press the toolbar button that looks like a green plus (+) sign. Or press hotkey Ctrl-B. You should see "jGRASP: operation complete". continued on next slide... Exercise 1 - run Run the program: Press the toolbar button that looks like a running man. Or press hotkey Ctrl-R. You should see the program's output in the bottom console area. Exercise 2: Modify an existing program Modify your MyFirstProgram file to produce the following console output. Note the blank lines; you should include those in your output. Hello, world! I am taking CSE 142. I hope it is a lot of fun! The teachers are named Benson and Marty. I hope they give me a grade of 4.0! Exercise 3: Practice turning in a program In CSE 142 you'll use a web turnin system for your homework. Let's practice turning in a file by submitting your MyFirstProgram.java. Go to the CSE 142 Labs web page. Click the link that says "Turnin Page for Exercise 3." Log in to the page and input your personal information. Browse to your MyFirstProgram.java file and submit it. Look at the turnin receipt page. Does it give you any warnings or errors? If so, fix them and try again. (A common error is giving a program/file the wrong name.) Exercise 4: Practice verifying output Part of your homework grades come from producing correct output exactly. Use our Output Comparison Tool web page to check if your output is correct. Go to the CSE 142 Labs page. Click the "Output Comparison Tool link." Select Exercise 4 from the list. The expected output should appear. Go back to your program. Run it and copy/paste its output from jGRASP's console into the "Actual Output" box of the web page. Click Compare on the page. Does your output match? If not, fix the program and try again. If you had to fix any output, turn in the program again. Exercise 5: Indentation Programs should be indented properly to make them easier to read. { brace → increase indent of following lines by one tab } brace → decrease indent of that line and following lines by one tab The following program has poor indentation. Paste it into jGRASP and fix it. public class Icky { public static void main(String[] args) { System.out.println("Properly indented programs"); System.out.println("look ever so much better!"); System.out.println("please fix me"); System.out.println("and make me beautiful again"); } } Exercise 6: Practice the Indenter Tool In CSE 142 you can use a web page called the Indenter Tool to automatically correct the indentation of a program. Go to the CSE 142 Labs web page. Click the link that says "Indenter Tool." Go back to your program. Select all of the code and copy/paste the code into the text box of the web page. Click Fix Indendation on the page. The code should now be indented correctly. Copy/paste it back into jGRASP and save the file. Exercise 7: What's the output? How many lines of output are produced (including blank lines)? public class Tricky { public static void main(String[] args) { System.out.println("Testing, testing,"); System.out.println("one two three."); System.out.println(); System.out.println("How much output"); System.out.println(); System.out.println("will there be?"); } } Answer: 6 lines. The blank lines in the code don't count, but the System.out.println(); statements do. Exercise 8: Syntax errors The following program contains 11 errors! What are they? 1 2 3 4 5 6 7 8 9 public class Tricky public static main(String args) { System.out.println(Hello world); system.out.Pritnln("Do you like this program"?); System.out.println() System.println("I wrote it myself."; { } Once you think you've found the errors, create/compile/run a corrected version of this program. answer on next slide... Exercise 8 - answer line 1: missing { after Tricky line 2: missing void before main line 2: missing [] after String line 3: missing " marks around Hello world line 4: system should be System (uppercase S) line 4: Pritnln should be println (lowercase P and fixed spelling) line 4: ? should be before " line 5: missing semicolon after () line 7: missing ) after " line 8: System.println should be System.out.println line 8: { should be } Exercise 8 - corrected version Here is a corrected version of the program: public class Tricky { public static void main(String [] args) { System.out.println( "Hello world "); System.out. println("Do you like this program ?"); System.out.println() ; System. out.println("I wrote it myself." ); } } Exercise 9: Exploring syntax errors Discover what error messages the compiler produces when you make each of the following mistakes. How many unique error messages are you able to cause the compiler to produce? Naming your file incorrectly, then compiling. Forgetting a keyword such as void or class Forgetting a quotation mark " Forgetting a parenthesis ( or ) Forgetting a dot . Using too many or too few braces { or } Notice that the error messages don't always make it obvious what is wrong. But they usually tell you the right line number to fix. Advanced: Escape sequences An escape sequence inserts a special character into a println statement. Sequence Special character \n new-line (goes to the next line) \t tab (indents output by roughly 8 spaces) \" quotation mark \\ backslash Example: System.out.println("I said \"hello\" to Fred."); Exercise 10: What's the output? (2) What output is produced by the following code? System.out.println("Shaq is 7'1"); System.out.println("The string \"\" is an empty message."); System.out.println("\\'\"\\\\\""); (Try to figure it out without running the code. If you give up, paste it into jGRASP and run it.) Answer: Shaq is 7'1 The string "" is an empty message. \'"\\" Exercise 11: Escape sequences Write a complete Java program that produces the following output (note the blank line): A "quoted" String is 'much' better if you learn the rules of "escape sequences." Also, "" represents an empty String. Don't forget: use \" instead of " ! '' is not the same as " (You can check your output on the Comparison Tool web page.) Exercise 12: Spikey Write a complete program that produces the following output: \/ \\// \\\/// ///\\\ //\\ /\ (Use only the material we have learned so far. You can check your output on the Comparison Tool web page.) Exercise 13: Spell Your Name Write a complete program that outputs your name in giant letters like the following: M M IIIII SSSSS PPPPPP MM MM I S S P P M M M M I S P P M M M I SSSSS PPPPPP M M I S P M M I S S P M M IIIII SSSSS P (Use only the material from Chapter 1. You can check your output on the Comparison Tool web page.) If you finish them all... If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook. You can view an exercise, type a solution, and submit it to see if you have solved it correctly. Choose some problems from Chapter 1 and try to solve them! Write a program to spell out MISSISSIPPI using block letters like the following (one per line):