Business Programming

Course announcements for INFS1609 and INFS2609

Business Programming random header image

Lab Week 3 – Data Types

Posted by on July 28th, 2012 · Labs

The exercises below use 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? 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

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

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)

 

No Comments so far ↓

Comments are closed.