Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CSE 142 Lab 1: Java basics University of Washington, CSE 142 Lab 1: Java basics University of Washington, CSE 142 Lab 1: Java basics Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp. lab document created by Marty Stepp, Stuart Reges and Whitaker Brand Basic lab instructions Mouse over highlighted words if you're not sure what they mean! Talk to your classmates for help. 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. Complete as much of the lab as you can within the allotted time. You don't need to keep working on these exercises after you leave. Feel free to complete problems in any order. Make sure you've signed in on the sign-in sheet before you leave! 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 : 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 this 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 - 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 - 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("Hooray!"); } } continued on next slide... Exercise - 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 - 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 - 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 : 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 learning to program in Java. I hope it is a lot of fun! I hope I get a good grade! Maybe I'll change my major to computer science. Exercise : 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 turnin exercise." 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 : 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 MyFirstProgram 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 : Practice 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 Example: public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println("How are you?"); } } Make sure that your MyFirstProgram program has good indentation. continued on the next slide... Exercise : Practice indentation, cont'd Our Indenter Tool web page can fix a program's indentation. In this exercise, we will use the Indenter Tool to fix the following program that has poor indentation. Download it and open it in jGRASP: Icky.java continued on the next slide... Exercise : Practice indentation, cont'd Go to the CSE 142 Labs page. Under Lab 1, click Indenter Tool. Copy/paste the Icky.java code into the Indenter's text box. Click Fix Indentation. (Indent by 3 spaces.) Paste the properly indented code in the box below. public class Icky { public static void main(String[] args) { System.out.println("Well-indented programs"); System.out.println("look much better."); System.out.println("Please fix me"); System.out.println("so I look nicer"); } } Exercise : 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 : Syntax errors The following program contains 11 errors! Work with a partner to find them all. You may want to run the program and jGRASP and see what kinds of error messages are shown for each kind of mistake. 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 - 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 - 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 : 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. 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 : What's the output? 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.) Note: when you see a purple check mark as in the upper-right corner of this page, that means that this problem is also available in PracticeIt by clicking on the check mark. Answer: Shaq is 7'1 The string "" is an empty message. \'"\\" Exercise : MuchBetter Write a complete Java program named MuchBetter 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 Output Comparison Tool web page.) Exercise : Spikey Write a complete program named Spikey that produces the following output: \/ \\// \\\/// ///\\\ //\\ /\ (Use only the material we have learned so far. You can check your output on the Output 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 the book and try to solve them!