Syntax Quiz 2

The quiz as given in class was as follows:

Write Java code for a class named "QuizClass". The class should have one instance variable, named "instanceVar", of a type suitable to hold an integer. The class should also have one method, named "doSomething", which takes in a string parameter named "theParameter" and returns nothing. The method should have a local variable named "localVar" of a type suitable to hold a floating-point number like 3.7524. The method will print out the value of "theParameter", appropriately labelled. It will then set "instanceVar" to be the length of "theParameter", set "localVar" to be the square root of "instanceVar", and print out both of these, also appropriately labelled.

Most of you did really poorly on this quiz, so I'm going to convert it into a step-by-step on-line self-tested lesson. For each step, write down your solution on paper, then follow the link to my solution and comments.

Let's break the problem into the following sequence of steps:

  1. Write a class declaration for a class named QuizClass. My solution
  2. Write a variable declaration for an instance variable named instanceVar, of a type suitable to hold an integer. My solution
  3. Insert the instance variable declaration into the class body. My solution
  4. Write a method definition for a method named doSomething that returns nothing, takes no parameters, and does nothing. My solution
  5. Insert the method definition into the class body. My solution
  6. Write a variable declaration for a parameter variable named theParameter of a type suitable to hold a character string. My solution
  7. Insert the parameter declaration into the method header. My solution
  8. Write a variable declaration for a local variable named localVar of a type suitable to hold a floating-point number. My solution
  9. Insert the local variable declaration into the method body. My solution
  10. Write a statement to print the words "The parameter is ", followed by the value of the variable theParameter. My solution
  11. Insert the statement into the method body. My solution
  12. Write an expression to find the length of the string stored in the variable theParameter. My solution
  13. Write an assignment statement to set instanceVar to this value. My solution
  14. Insert the assignment statement into the method body. My solution
  15. Write an expression to find the square root of the number stored in the variable instanceVar. My solution
  16. Write an assignment statement to set localVar to this value. My solution
  17. Insert this assignment statement into the method body. My solution
  18. Write a statement to print the words "The parameter's length is ", followed by the value of instanceVar. My solution
  19. Insert this statement into the method body. My solution
  20. Write a statement to print the words "Its square root is ", followed by the value of localVar. My solution
  21. Insert this statement into the method body. My solution

Last modified:
Stephen Bloch / sbloch@adelphi.edu