Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

Computer Science Courses

Related sites

Introductory Programming In Java

Catch "Little" 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.

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:

  1. Predict what the following code will produce for the cases of xinitialised 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!");
  2. 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!");
  3. 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!");
  4. 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!");
  5. 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?");   }
  6. 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?");   }
  7. 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!

  1. 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);
  2. 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);
  3. 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);
  4. 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);
  5. 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);
  6. 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);
  7. 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);
  8. 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);
  9. 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.

  1. 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;   }
  2. 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;   }
  3. 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++;   }
  4. 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--;   }
  5. 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;   }
  6. 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;   }
  7. 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;   }
  8. 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!)

Construct some classes

  1. Imagine that you are running a used car yard and you want to manage aninventory of cars.

    1. Write a Car class to store information about a particular car:
      1. Fields in this class should be:
           serialNumber    maker   dateOfManufacture   dateOfAcquisition   acquisitionCost   salePrice    condition
      2. Make the field condition a String with three possible values: "good", "bad", "average"
      3. Store dates using the Java java.util.Date class:
    2. Write constructors and get and set methods for your Car class
    3. From a main method in another class, fill out an inventory array of 6 cars
    4. Print out your inventory. Make it look nice (ie, use System.out.printf() method)
    5. 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.)
    6. Play with this system and make it more realistic.
  2. Imagine that you are maintaining the stock of a vending machine:

    1. Write a VendingProduct class to store information about your machine with the fields:
         serialNumber    productName   positionNumber   quantity   price
    2. Write constructors and get and set methods for your VendingProductclass.
    3. Add a field called minimumNumber, and a method checkQuantity which returns false if the quantity ofa given product falls below the minimum number.
    4. From a main program in another class, fill out an inventory of 6 products. Have 10 units of each product.
    5. Print out your inventory. Make it look nice.
    6. Play with this system and make it more realistic.

     

    Updated:  Sun 12 Jun 2016 17:27:37 AEST Responsible Officer:   JavaScript must be enabled to display this email address. Page Contact:  

      +61 2 6125 5111
      The Australian National University, Canberra
      CRICOS Provider : 00120C       ABN : 52 234 063 906