Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Week 05: Drop-In Lab - 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 05: Drop-In Lab Objectives To drill some aspects of Java programming. These exercises are all optional. There are also many other good exercises available in your Java text book and online (some of these are pointed to below.) You are welcome to attempt any of these other exercises during supervised lab sessions and seek help and feedback on them. Warning: These exercises are basic, long and maybe tedious. Language Drills: if statements Do as many or as few of the following exercises as you want — until you get sick of them! The following drills relate to code snippets which contain if statements. For each question, test your answer out by coding up a simple main method: Predict what the following code will produce for the cases of x initialised to be 49, 50 and 51. Now run the code and see that you were correct. int x = 49; if (x <= 50) { System.out.println("I am here!"); } if (x >= 50) { System.out.println("Am I here?"); } System.out.println("Here I am!"); Predict what the following code will produce for the cases of x initialised to be -49, -50 and -51. Now run the code and see that you were correct. int x = -51; if (Math.abs(x) <= 50) { System.out.println("I am here!"); } if (Math.abs(x) >= 50) { System.out.println("Am I here?"); } System.out.println("Here I am!"); Predict what the following code will produce for the cases of x initialised to be 49, 50 and 51. Now run the code and see that you were correct. int x = 49; if (x == 50) { System.out.println("I am here!"); } if (Math.abs(x) >= 50) { System.out.println("Am I here?"); } System.out.println("Here I am!"); Predict what the following code will produce for the cases of x initialised to be 49, 50 and 51. Now run the code and see that you were correct. int x = 51; if (x == 50) { System.out.println("I am here!"); } if (x >= 50) { System.out.println("Am I here?"); } System.out.println("Here I am!"); Predict what the following code will produce for the cases of x initialised to be 49, 50 and 51. Now run the code and see that you were correct. int x = 49; if (x < 52) { System.out.println("I am here!"); } else if (x < 51) { System.out.println("Am I here?"); } else if (x < 50) { System.out.println("Here I am?"); } Predict what the following code will produce for the cases of x initialised to be 49, 50 and 51. Now run the code and see that you were correct. int x = 49; if (x > 52) { System.out.println("I am here!"); } else if (x > 51) { System.out.println("Am I here?"); } else if (x > 50) { System.out.println("Here I am?"); } Predict what the following code will produce for the cases of x initialised to be 49, 50 and 51. Now run the code and see that you were correct. int x = 49; if (x < 52) { System.out.println("I am here!"); } else if (x > 51) { System.out.println("Am I here?"); } else if (x >= 50) { System.out.println("Here I am?"); } Language Drills: Boolean Expressions Do as many or as few of the following exercises as you want — until you get sick of them! Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. boolean finished = false; boolean again = false; System.out.println(!finished); Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. boolean finished = false; boolean again = false; System.out.println(!!!finished); Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. boolean finished = false; boolean again = false; System.out.println(!!!!!finished); Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. boolean finished = false; boolean again = false; finished++; System.out.println(finished); Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. boolean finished = false; boolean again = false; System.out.println(finished && again); Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. boolean finished = false; boolean again = false; System.out.println(finished || again); Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. boolean finished = false; boolean again = false; System.out.println(finished || !again); Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. boolean finished = false; boolean again = false; System.out.println(!finished || !again); Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. boolean finished = false; boolean again = false; // My goodness! Look at this one...! System.out.println((finished || !again) && (finished || (again || !again))); Language Drills: Loops Do as many or as few of the following exercises as you want — until you get sick of them! The following drills relate to code snippets which contain while loops. For each question, test your answer out by coding up a simple main method. You could also try modifying some of the loops to be for loops or do-while loops. Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. int x = 0; while (x < 100) { System.out.println(x); x = x + 10; } Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. int x = 0; while (x <= 100) { System.out.println(x); x = x + 10; } Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. int x = 0; while (x <= 100) { System.out.println(x); x++; } Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. int x = 100; while (x <= 100) { System.out.println(x); x--; } Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. int x = 0; while (x <= 100) { System.out.println(x); x += 30; } Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. int x = 1; while (x <= 100) { System.out.println(x); x *= 30; } Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. boolean isFinished = false; int x = 0; int counter = 0; int maxCounter = 13; while (!isFinished) { System.out.println(x); x = (x + 30) % 100; counter++; if (counter > maxCounter) isFinished = true; } Predict what the following code will do, and what it will print out. Now run the code and see that you were correct. boolean isFinished = false; int x = 0; int y = 0; int counter = 0; int maxCounter = 13; while (!isFinished) { System.out.println(x); x = (x + 30) % 100; y = (y + 30) % 150; counter++; if (counter > maxCounter) isFinished = true; } Online coding practice There are some online sites for coding practice. (The first one in particular is a lot of fun!) http://codingbat.com/java https://www3.ntu.edu.sg/home/ehchua/programming/java/J2a_BasicsExercises.html https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html#ExerciseClass http://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/en/objects.html https://www.caveofprogramming.com/java/beginners-java-test-your-knowledge-of-classes-and-objects.html Construct some classes Imagine that you are running a used car yard and you want to manage an inventory of cars. Write a Car class to store information about a particular car: Fields in this class should be: serialNumber maker dateOfManufacture dateOfAcquisition acquisitionCost salePrice condition Make the field condition a String with three possible values: "good", "bad", "average" Store dates using the Java java.util.Date class: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html Write constructors and get and set methods for your Car class From a main method in another class, fill out an inventory array of 6 cars Print out your inventory. Make it look nice (ie, use System.out.printf() method) Apply a discount of 10% to all of your "bad" cars and print out the inventory again. (Note that you might need to add additional methods to your Car class.) Play with this system and make it more realistic. Imagine that you are maintaining the stock of a vending machine: Write a VendingProduct class to store information about your machine with the fields: serialNumber productName positionNumber quantity price Write constructors and get and set methods for your VendingProduct class. Add a field called minimumNumber, and a method checkQuantity which returns false if the quantity of a given product falls below the minimum number. From a main program in another class, fill out an inventory of 6 products. Have 10 units of each product. Print out your inventory. Make it look nice. Play with this system and make it more realistic. Updated:  21 Feb 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