Home : Course Map : Chapter 3 : Java :
Exercises
JavaTech
Course Map
Chapter 3

Introduction
Class Definition
Data Fields
Methods
Constructors
Instantiation
  
Demo 1
  Demo 2
Static Members
  Demo 3

Value&Reference
  Demo 4
Overloading
   Demo 5
Wrappers 
  Demo 6
Autobox/Unbox
   Demo 7
Arrays
  Demo 8
Exceptions
Exercises

    Supplements
MoreAboutObjects
Creating Types

Classes,Objs&JVM

Java OOP vs C++
Garbage Collection
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

These exercises illustrate various basic aspects of programming with classes and objects.

  1. Create an applet with two parameters in the web page tag consisting of an integer and floating point value. Create an applet that performs the following.

    • The integer parameter takes one of three different values, say 0, 1, and 2. Test on the integer parameter to decide which of 3 different methods to call.

    • Each of these methods should carry out a different calculation with the floating point parameter.

    • Print out a message from each method indicating the method chosen and the result obtained.

  2. Create an application with two attached classes, e.g. Test1 and Test2, in the file instead of the one class Test as in the demos in this chapter. Test1 should contain a single method and Test2 just holds a single data variable.

    Create an instance of each class in the main() method. Call the method in Test1 with a reference to Test2 in the argument list. In that method, alter the value of the variable in the Test2 object. When the process returns to main(), print out the value of the Test2 variable.

  3. Create a static class with a method to calculate the area of circle using the radius passed in the argument.

  4. For the WrapperApplet demonstration program,
    1. Put in a non-number string in the applet tag for the first parameter and run the applet.
    2. Re-write the applet to "catch" the error, print a error message, and use a default value for "val1".
    3. Repeat for the second parameter, i.e. use a non-boolean string in the applet's second tag, catch the error, and use a default value.

           Tech
OOP in Tech Apps
Complex Number
Histogram Class
  Demo
More Wrappers

           Physics
OOP in Physics
Particle Class
Root Finding
  Demo 1
Newton Methods
  Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.