Q4 Input Questions

1. What type of variables would hold the following data? (int, double, String char)

    1. the price of a box of Kraft Dinner
    2. a user's last initial
    3. the grade you are in
    4. a user's name
    5. the first letter of the name of the month
    6. pi
    7. the jersey number of a hockey player
    8. the distance from Toronto to Kitchener
    9. the number of chairs in a classroom
    10. the name of the Prime Minister
    11. the amount of GST owing on a grocery bill

2. For each of the descriptions in part 1, what method from the IBIO class would be needed to input a value into the variable? (IBIO.inputString, IBIO.inputChar, IBIO.inputInt, IBIO.inputDouble)

3. For each of the description in part 1, what would be a good variable name?

4. Declare a variable, and use IBIO to get the following information. eg. String name = IBIO.inputString("Enter your name:");

      (a) the length of a side of a square
      (b) the total price of a shirt
      (c) the tax rate
      (d) the answer to “Do you want to continue(y/n):”
      (e) a menu:

      Pick one of:
         1. Introduction
         2. Argument
         3. Conclusion
         4. Recommendation
         5. Quit

      (f) the first letter of the user’s name

 5. Write programs to do the following:

      (a) Ask the user the length of the side of a square and output its area
(b) Ask the user for their favourite letter and then output a line of 20 of them
(c) Ask the user for the height and base of a triangle and calculate its area
(d) Prints a menu, asks for a selection and then repeats the number chosen to the screen. (eg of repeat: You have choosen 6)

6. Which line has an example of each of the following: (look at your code file for examples)

    1. assignment statement
    2. declaration of a variable
    3. input statement
    4. output statement
    5. String (item in double quotes)
    6. main method (make the windom)
    7. constructor method
    8. constructor call
  1. public class midPoint
  2. {
  3. public static void main (String args [])
  4. {
  5. new midPoint ();
  6. }
  7. public midPoint ()
  8. {
  9. System.out.println ("MID POINT PROGRAM");
  10. System.out.println ("");
  11. System.out.println ("We will need the two co-ordinates:");
  12. System.out.println (" (x1, y1) and (x2, y2)");
  13. System.out.println ("");
  14. int x1 = IBIO.inputInt ("Please enter x1 >> ");
  15. int y1 = IBIO.inputInt ("Please enter y1 >> ");
  16. System.out.println ("");
  17. int x2 = IBIO.inputInt ("Please enter x2 >> ");
  18. int y2 = IBIO.inputInt ("Please enter y2 >> ");
  19. System.out.println ("");
  20. int midx = (x1 + x2) / 2;
  21. int midy = (y1 + y2) / 2;
  22. System.out.println ("The midpoint is (" + midx + ", " + midy + ").");
  23. }
  24. }

Debugging and Variables - A Short Exercise

Question 6: There are 5 distinct errors in the following java program, what are they?
 

import java.io //line1

public class  //line2
{ //line 3
   public static main (String args[]) //line 4
   { int h; //line 5
     h=IBIO.inputDouble("Please enter a decimal:"); //line 6
      System.out.println(num); //line 7
   } //line 8
} //line 9

Question 7. Which type of variable is used for each of the following situations? (int, double, char, String)
(a) To hold a person's name
(b) To hold the answer to "Enter the first letter of your name"
(c) The hold the price of a hamburger
(d) To hold the number of students in a class
(e) To hold the answer to "Is this correct (y/n)?"
(f) To hold the amount of tax on a purchase
(g) To hold the mark someone got on a test
(h) To hold the number of fish in a tank
(i) To hold the day of the month
(k) To hold "Turner Fenton"