Exercise 2

Primitive data types (PDT) are often used as the basic building blocks of
information that make up objects and other Abstract Data Types (ADT) in JAVA. A
PDT can be an integer, real number, character, or a true or false value. Often
PDTs in Java are used as the attributes of a class. All Primitive data types contain
a numeric ...

Part of the document


Introduction to Classes
Introduction to Primitive data types,
processing of numeric data and Methods.

Exercise 2
Prerequisite:
1. Read Sections 2.1, 2.2, and 2.3 before attempting this exercise.
2. Read this document first and Appendix A on page 6 and Appendix B
on page 8 before doing this exercise.
3. You will not be able to complete this exercise by reading this
document alone. All Exercises will be required for completion of
project 1 and the final project.

Bare necessities of a Computer Program

Classes in JAVA

A class in Java is used to describe an object. These descriptions
are used to inform the compiler the make up of each instance of the class
or Object that is created. Each class will contain attributes that
describe what an object should be and methods that conduct actions on the
data or on behalf of the object.
Classes in Java are much like blueprints to a building. The hold the
instructions on what an object is and what an object can do. The
Attributes can be made up of Primitive data types as well as other objects.
Methods may provide access to the data types to change the data or
retrieve values stored in memory for use by other objects in the Java
program.
The class "Body" is often identified by the class heading[?] and
enclosed by opening curly braces and closing curly braces. The "data
field"[?] declarations or attributes usually appear within the class "Body"
near the top of the class definition[?]. The data field declarations or
attributes often are made up of Primitive data types or other objects
references.
After the attributes of the class the method definitions are then
placed within the body of the class. Each method also contains curly
braces that define the "body of the method".
Each class name must mach the name of the file given by the
programmer. This is a convention of Java and is strictly enforced by the
compiler.
Every program in this course will require at least two classes, the
main implementation class and a class that the student defines. Although
at least two classes are required each student may feel free to add classes
as they are needed.


Primitive Data Types (PDT) in JAVA

Primitive data types (PDT) are often used as the basic building
blocks of information that make up objects and other Abstract Data Types
(ADT) in JAVA. A PDT can be an integer, real number, character, or a true
or false value. Often PDTs in Java are used as the attributes of a class.
All Primitive data types contain a numeric value that the computer will
understand. This includes characters as well as Boolean values[?].


Types of PDTs
|1. int |2. double |3. float |
|4. char |5. Boolean |6. byte |
|7. long |8. short | |

Casting
Change the values from one data type into another. Each data
type stores numeric data differently so that the computer can understand
the numeric data stored in memory. For example, an integer is store in
less space in memory than a double. A double also contains a special
representation of real numbers that integers lack. This is the decimal
point and the way the computer stores this information for double data
types are different than that of the integer. The following is an example
of how to cast an integer value into a double.
double Example = (double)7;
The above example changes the value of the integer 7 into the
double 7.0. The casting changes the value of the integer 7 into the real
number representation of 7.0.

Methods in JAVA
There are different types of methods in JAVA.
1) Constructors
2) Accessor
3) Mutator/Modifier
4) The main method

The main method is a special method in a JAVA program. This method
is always in the main implementation class and denotes where the computer
program begins. The flow of the program is started and ends in the main
method.
Let's examine the main method in the exercise in Appendix A. The
program will first create an instance of the class Adder[?] called MyAdder.
MyAdder is the name that is given to the object of type Adder. The
program then sets the attributes of MyAdder to the values in the
parenthesis. The program then will print the value of the attribute of
MyAdder called answer as a character. Then the program ends.

Exercise 2

Write a program that will take two integers, add them together and output
the answer as a character. Use of the software development method is
required as shown in exercise 1.

Observations[?]
Type in the program provided in Appendix A on page 6. After you have
compiled and successfully run the program try the following steps.
1. Remove the semicolon after MyAdder.SetAdder(65,3) > note the compiler
error
2. Change MyAdder.SetAdder(65,3) to MyAdder.SetAdder(3) > note the
compiler error. Why do you think that this is an error?
3. Remove the // in front of the word copyright and compile this file. >
note the compiler error. Why do you think that this is an error?
4. Remove the closing curly brace at the end of the class Adder. > note
the compiler error.
Review of Exercise 2

Key Words

|Accessor methods |Mutator/Modifier |Constructor |
| |methods | |
|method |Attribute |class |
|class body |method body |main |
|Instance Method |Class Method | |






Review Questions

1. What are comments used for in a computer program?
2. List 5 Primitive data types?
3. List 4 JAVA keywords that are not PDTs?
4. Explain what "dot notation" means?
5. Where are attributes of a class usually defined?
6. What are used to denote the beginning and ending of a method body?
7. Give two ways that you can make a comment in a Java Program?

Review Exercise[?]

1. Create a Subtraction class that will accept two integers and output the
answer as a character. Use of the software development method is required.

2. Create a class that will take a letter, add 10 to the letter and output
the answer as a character.

Appendix A

Exercise 2 Program

Note: Each class in JAVA requires a separate
file and the class name must be the same as the filename.
The "Extension of the filename must be .java"[?]
File one the main implementation file[?]

//Program Name: Add Two Numbers

//By: Prof. Re
//CMP210
//Exercise 2
//Prof. Re

//Copyright 2003

public class MyProg
{
public static void main(String[] args)
{
Adder MyAdder = new Adder();

MyAdder.SetAdder(65,3);
MyAdder.printChar();
}
}
File Two the Adder Class
A Typical class in a JAVA program
//Class Name: Adder
//Class Description:
// This class will take the two numbers as attributes[?] and
store the
//two numbers in the attribute Answer. This class provides methods to set
the numbers, //and output the answer to standard out as a character.
//By: Prof. Re
//CMP210
//Exercise 2
//Prof. Re
public class Adder
{
private int num1;
private int num2;
private int Answer;



public Adder()
{
num1 = 0;
num2 = 0;
Answer = num1 + num2;
}


public void SetAdder(int number1, int Number2)
{
num1 = number1;
num2 = number2;
Answer = num1 + num2;
}

public void printChar()
{
System.out.println((char)Answer);
}

public int GetAnswer()
{
return Answer;
}
}
Appendix B
Methods Explained

Method Headings

public void SetAdder(int number1, int Number2)
{
num1 = number1;
num2 = number2;
Answer = num1 + num2;
}















































Note: Because this method has the visibility of public, all other methods
and objects can have access to this method. This Particular method is
considered to be a Mutator Method because it is going to change the values
of the attributes to those passed to it through the parameter list
Works cited
[1] Please see Appendix A on Page 6
[2] Although attributes can go near the bottom of the class definition it
is not done so hear to follow the conventions of the text.
[3] To learn more about Primitive data types, please read the text.
[4] or object
[5] Note the following are common mistakes that have been observed with
students writing programs. Take your time with this and take a look at the
errors that are created from doing each step. Knowing what the compiler
error messages mean is more than half the battle with programming projects.
[6] No user input is required for this exercise and will be covered in
exercise 3
[7] Program execution begins at the beginning curly brace of the program
body and ends at the end curly brace of the program body.
[8] Though we are using int as the data type for this class, this does not
have to be the case for attributes of other classes that you create.
Attributes can be of any primitive data type or object.

[i] Problem Solving with Java, Elliot B. Koffman, Ursula Wolz, Addison
Wesley, New York 2002, page 77
[ii] An Introduction to Computer Science Using Java second Edition , Samuel
N. Kamin, M. Dennis Mickunas, Edward M. Reingold, Mc Graw Hill, New York,
2002 page 32

--