Did Chapter 6 of:
SCJA—Sun Certified Java Associate Certification Study Guide for Java 5, Java2EE and J2ME Technology form ExamScam.com
this morning. I created an ExamField Java project in Eclipse to run ExamField. This way I could compile, run and debug scenarios in real time. I learned a couple of things. The primitive data type CHAR must go inside single quotes.
------------------------------------------------------------------------------------------
public class TestFieldChar {
public static void main(String[] args) {
char singleCharacter = 'a';
System.out.println("Test Field");
System.out.print(singleCharacter);
}
}
OUTPUT:
Test Field
a
---------------------------------------------------------------------------------------
I thought that there may be an issue when compiling the output from the following example for BYTE:
CODE:
public class TestFieldByte {
public static void main(String[] args) {
byte b = 12;
System.out.println("Test Field");
System.out.print(b * b);
}
}
OUTPUT:
Test Field
144
I presumed that the compiler would flag the output and go out of scope, as the maximum value for BYTE is 127. 12 * 12 is 144 and out of scope for BYTE data types. In this case, all was well. However, if you had a variable of type byte assigned to the output of b * b, you would go out of scope.
byte result = b * b, then result would be out of scope as it has been declared data type byte.
-----------------------------------------------------------------------------------
BOOLEAN data types have no quotes, single quotes or bracketing when assigning. Only possible values are true and false.
CODE:
public class TestFieldBoolean {
public static void main(String[] args) {
boolean condition = true;
System.out.println("Test Field");
System.out.print(condition);
}
}
OUTPUT:
Test Field
true
------------------------------------------------------------------------------------
10 comments:
Interestingly, when you multiple two whole number bytes or shorts, the compiler assumes an int is returned. So this is fine:
byte a =1;
byte b = 2;
System.out.println(a*b);
BUT, this will cause a compile error:
byte c = a*b;
to make it compile, you either cast:
byte c = (byte)a*b;
or use an int:
int c = a*b;
Neat stuff.
Here's an assignment for you. This is important to do, because it will really hit where you're getting frustrated - working with methods, both creating them and passing values to them. Give it a try!
I didn't compile the code in the examples, so you might want to make sure I haven't missed a semi-colon or anything. Regardless, the objective remains the same: create the methods that I've asked for, and write some code that invokes the methods by passing in the appropriate objects! This will be eye-opening for you.
****************************
Let's look at three core shapes: Circle, Point and Rectangle.
Here's how we create them:
Circle drum = new Circle(2);
Circle life = new Circle(10);
Point ed = new Point(0,0);
Point pelee = new Point(5,5);
Rectangle r1 = new Rectangle(2,3);
Rectangle r2 = new Rectangle(5,4);
If I wanted to just print out the area of a Circle, I could create a method like this:
public class PracticeMethods {
public void printCircleArea(Circle c) {
System.out.println("The area of the circle you passed me is: " + c.getArea();
}
}
To run this code, I could write a class like this:
public class PMRunner {
public static void main(String args[]) {
Circle drum = new Circle(2);
PracticeMethods pm = new PracticeMethods(); //create an instance
pm.printCircleArea(drum); //pass an instance of a Circle to the method.
}
}
When the PMRunner executes, it will print out: The area of the circle you passed me is: 6
So, here you see how to code a class that has a method that takes an argument. (The PracticeMethods class)
And you also see how to create an instance of a class, and call one of the instance methods, while also passing in an argument of the proper type.
Question: Why does the printCircleArea return void and not a String? Doesn't the method return a String?
In the PracticeMethods class, create the following methods:
compareCircleToCircle - it takes two circles as arguments. It returns true or false depending on whether the two circles have the same area.
comparePointToCircle - it is passed in a Point and a Circle. It prints out how much bigger the area of the circle is than the area of the point.
compareCircleToSquare - it is passed in a Circle and a Square. It returns a String that says "The circle is bigger than the square" or "The square is bigger than the circle" or "The circle and square are the same size."
compareSquareToSquare - it is passed in two Squares, and returns a boolean value indicating if the first square is bigger than the second. It also returns the difference in the sizes of the two squares.
*************************
Ok, I may need some help before progressing. I have written two classes, PracticeMethods and PMRunner and cut and pasted the corresponding code to both classes as seen here. I am getting compile errors in both classes. Both classes cannot resolve types (Circle in both classes and PracticeMethods in the PMRunner class). Here are my classes:
PMRunner:
public class PMRunner {
public static void main(String args[]) {
Circle drum = new Circle(2);
PracticeMethods pm = new PracticeMethods(); //create an instance
pm.printCircleArea(drum); //pass an instance of a Circle to the method.
}
}
PracticeMethods:
public class PracticeMethod {
public void printCircleArea(Circle c) {
System.out.println("The area of the circle you passed me is: " + c.getArea());
}
}
I believe the errors correspond with neither Circle nor PracticeMethod being declared and initialized.
Also, I do not have the core shapes on this computer. I have no Circle.java or Circle class.
I think we need to stop using four computers so that all of the code remains in one place!
Or, could we not post our Eclipse workspace to one of our servers? I have extra space @ realtimerenovations.com
Ok, I am here:
PROGRAM OUTPUT:
Circle drum radius 2
Circle life radius 10
The area of the circle named Circle@addbf1 you passed me is: 12
The area of the circle named Circle@42e816 you passed me is: 300
The two circles do not compare
The two circles are the same
Classes:
CIRCLE:
public class Circle {
public int radius;
public Circle(int r) {
radius = r;
}
public int getArea() {
return radius*radius*3;
}
}
PMRUNNER:
public class PMRunner {
public static void main(String args[]) {
Circle drum = new Circle(2);
Circle life = new Circle(10);
System.out.println("Circle drum radius " + (drum.radius));
System.out.println("Circle life radius " + (life.radius));
PracticeMethod pm= new PracticeMethod(); //create an instance
pm.printCircleArea(drum); //pass an instance of a Circle to the method.
pm.printCircleArea(life);
pm.compareCircleArea(drum, life);
}
}
PRACTICE METHOD:
public class PracticeMethod {
public void printCircleArea(Circle c) {
System.out.println("The area of the circle named " + c + " you passed me is: " + c.getArea());
}
public void compareCircleArea(Circle d, Circle l){
int a = d.getArea();
int b = l.getArea();
if (a != b){
System.out.println("The two circles do not compare");
}
System.out.println("The two circles are the same");
}
}
I will get back to this, getting closer . . . . 8)
Just got the circles to compare:
public class PracticeMethod {
public void printCircleArea(Circle c) {
System.out.println("The area of the circle named " + c + " you passed me is: " + c.getArea());
}
public void compareCircleArea(Circle d, Circle l){
int a = d.getArea();
int b = l.getArea();
if (a != b){
System.out.println("The two circles do not compare");
}
if (a == b) {
System.out.println("The two circles are the same");
}
}
}
The method is displaying proper output (do the circle area's compare/not compare) as I pass two different instances of a circle to it.
Yay me!
Notice there is no main method in the PracticeMethods class. That always bugs you. But it's just a worker class filled with methods, or what VB calls 'functions.' It's the PMRunner that creates the objects and calls the methods in the PracticeMethods class.
And you are seeing the difference between the 'instance' and 'methods of the instance.'
The instances are pointing to that memory location, Circle@42e816 or whatever. The variable names we use are just easy ways for us as humans to differentiate between these different memory locations. So, if you just print out the variable name:
System.out.println(drum);
Then you get something like: Circle@42e816
But it's the area of the instance named drum we are looking for, so we call its method:
drum.getArea();
Always notice the round brackets at the end. Methods calls always have round brackets; sometimes there are arguments passed in (instances) and sometimes there is not, but they're always there.
Also notice the names of the two circles in the method are named d and l. That's the name you give it to use locally in that method. It doesn't matter what they're named in other places, as it's all pointing to the same memory location. It's the memory location, or the true data that represents the object, that is important, not the name we give it. If the object is in memory location Circle@42e816, we can give it ten different names, so long as each name points to that location, then each one represents the same instance.
'instance' is a very important term.
Question on primitive data types. If I declared a LONG data type, and initialized it to one of the two maximum spectrum ranges (-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807), what would happen with my RAM/the compiler on a computer who only has 2 gig's of RAM? Are these values not larger than the RAM iteself? Will the JVM use virtual memory (hard disk drive) in place of RAM if capacities are exceeded?
It makes no difference if you set it to 1, or set it to one million. Any time a long is created, the JVM puts aside 64 bits of memory for the value. Each bit is used; it's just that for smaller numbers, most of the bits are simply zeroes.
64 bits is 8 bytes (8x8). A gigabyte is 1,073,741,824 bytes. You'll need to declare a lot of longs before you start putting any stress on your memory.
Post a Comment