Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Week 02: Lab 1 - Introductory Programming in Java Skip navigation Introductory Programming in Java ANU College of Engineering & Computer Science Search query Search ANU web, staff & maps Search COMP6700 Lectures Labs Assignments menu Search query Search ANU web, staff & maps Search Search COMP6700 labs Week 01: Welcome Week 02: Lab 1 Week 03: Lab 2 and HW 1 Week 04: Lab 3 and HW 2 Week 05: Drop-in Lab, HW 3 Week 06: Lab 4 and HW 4 Week 07: Mid-sem Exam Week 08: Free (Anzac Day) Week 09: Lab 5 and HW 5 Week 10: Lab 6 and HW 6 Week 11: Lab 7 and HW 7 Week 12: Lab 8 and HW 8 related sites Wattle Piazza Week 02: Lab 1 Objectives We will learn how a Java program is compiled and executed (“run”) We will learn how a program can be passed data before execution We will learn about data types and simple operations which can be performed on data depending on their type Exercise One Download the free book How to Think Like a Computer Scientist, Java™ Version by Allen B. Downey, and do the Exercise 1.7 at the end of Chapter One, p.11-12: you have to type in (really, type it in, do not paste and copy!) the “Hello World” program (like it’s presented in the lectures or in the Downey’s book, p. 7). Compile (javac Hello.java) and run (java Hello) it. Then follow the advice of breaking the initial code (in Ex 1.3, a—i), to make small changes, and try to compile the program again. Read and think through the meaning of the compile messages. Exercise Two Write a program that takes two integers and prints out their sum, difference, product and average. Change the values of these integers by editing your program. Then make the program read the values of integers from a user as two command-line arguments (like we did in the lectures). The program execution on the command-line will, therefore, looks like this: % java MyProgram 3 5 8 -2 15 4 You should remember, that the command-line argument’s type is the String class, and before they can be used as integers (which is the intended goal here), these strings must be converted to integer types (the int type is one of the primitive types used to represent integer numbers). By peeking (only so slightly) ahead of the course material, use the following “transformation” trick (which is the standard way to convert a value of string type to the integer it is meant to represent; see more details in Hortsmann’s book Ch. 1.5.4, or the Converting Between Numbers and Strings in Java Tutorial): int x = Integer.parseInt(args[0]); int y = Integer.parseInt(args[1]); BTW, can you (do research if necessary) modify this program to make it print a well-formatted output, similar to this one? % java MyProgram 10 2 10 + 2 = 12 10 - 2 = 8 10 * 2 = 20 (10 + 2)/2 = 6 (Hint: find out how the method printf() can be used; this method is a useful alternative to the more simpler ones print() and println(). See the Java API documentation for the PrintStream class.) Exercise Three Write a program that calculates the number of characters in a string which the user types in. Write a program to remove the first character from a string which the user types in. Write a program to remove the last character from a string which the user types in. Note You will make your life simpler if you reuse a previous program to do the next task which may be similar (like in the Exercise Two). Copy an existing source file (on the Terminal this is done by the copy command: % cp OldClass.java NewClass.java and appropriate editing of the NewClass.java.) Organising your workspace It make a lot of sense to organise your home directory for the COMP6700 (or COMP2140) studies. Given the content of our activities (check the labs and deliverables in the Schedule), you can create a dedicated directory comp6700 (or, comp2140) in your Home Directory, with the following structure: comp6700 / | \ / | \ / | \ assignments hw labs / \ | / \ ass1 ass2 ... lab1 ... This can be done by merely using the window manager (on Linux, this application is called Files; launch it the same way you’ve launched Terminal or Atom), or alternatively (and more in the spirit of programming) by using the command-line tools like mkdir (again, refer to the Basic Command-Line Commands guide, and/or ask your tutor to demonstrate). Updated:  03 Mar 2017/ Responsible Officer:  Head of School/ Page Contact:  Alexei Khorev Contact ANU Copyright Disclaimer Privacy Freedom of Information +61 2 6125 5111 The Australian National University, Canberra CRICOS Provider : 00120C ABN : 52 234 063 906 You appear to be using Internet Explorer 7, or have compatibility view turned on. Your browser is not supported by ANU web styles. » Learn how to fix this » Ignore this warning in future