COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Lab Week 3 – Data Types

Posted by on August 8th, 2013 · Lab

This week’s lab exercises are concerned with data types, which were introduced in last week’s lectures.

Feedback on your progress

At the start of the lab, you’ll be given a short quiz on writing simple methods. This shouldn’t take more than 15 minutes.

Each week, we will ask you short quiz questions to encourage you to do some preparation before the lab. The intention of the quizzes is to give you quick feedback so that you know if you understand the material in the course so far and to encourage you to prepare for the labs by reading over the lecture material from the previous weeks.

You’ll be given a mark for each quiz just to make sure that you take them seriously, but note that they contribute only a small part to your final assessment (about half of your lab mark, the other half from your attempts at the lab exercises). Your answers will be marked as either satisfactory or unsatisfactory by your tutor during the lab. If you have a good reason for missing a quiz, talk to your tutor about making it up.

After completing the quiz, you should attempt the exercises below using the Tut3.zip project for BlueJ.

Data types

What data type would you use to represent the following values? Why?

  1. The length of a piece of string.
  2. The number of students at UNSW.
  3. A person’s age.
  4. The mass of the sun.
  5. The exact number of stars in the Milky Way.
  6. The mathematical constant π.

Expressions

What are the value and type of the following expressions? Work these out on paper and then test your answers by typing them into the Code Pad in BlueJ. Can you explain the result?

  1. 5 / 2
  2. 5 % 2
  3. 5.0 / 2
  4. 5 / 2.0
  5. 5.0 / 2.5
  6. 5.0f / 2
  7. 1.5 % 1
  8. 1 / 0
  9. 1.0 / 0
  10. 10000000000 + 1
  11. 10000000000L + 1
  12. 10000000000.0 + 1
  13. 10000000000.0f + 1
  14. int x = 10;
  15. x = x + 2;
  16. int y = 3;
  17. int z = x + y + 1;
  18. x = x+ 2;

Circle area

The circleArea method in the Tut3 class calculates the area of a circle with a given radius.

  • Create a new Tut3 object and try calling this method with different input values.
  • Add breakpoint on the first line and step through the code. Watch the ‘local variable’ panel to see variables being created and change value.
  • Change the method to calculate the area of an ellipse of given width and height. The area of an ellipse is π × height × width / 2. You can use the value 3.141592 for π or use Java’s math library definition, Math.PI.

Hand shaking

If there are five people Alan, Bel, Cameron, Dan, and Emily in the room and they each have to shake hands with everyone else, how many handshakes occur? What if there were 10 people? What if there were only two? Work out a general formula.

Complete the ‘handshakes’ method in the Tut3 project to use your formula to compute the number of handshakes for a given size group.

The Math Class

For more complicated mathematical expressions, Java has a lot of built in methods in the Math class. Take a look at the documentation to see what is available. Try typing the following
in CodePad. Explain the results.

  • Math.sqrt(2.0)
  • Math.round(3.1)
  • Math.round(3.7)
  • Math.min(1, 2)
  • Math.max(1, 2)

If-statements

Assume we have declared an integer variable x. Write an if-statement that will print “one” if the value of x is 1, “two” if the value of x is 2 and “something else” if the value x is some other value.

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

You must log in to post a comment.