menu :
- this keyword
- his keyword
- overloading and overriding
- polymorphism
- variable and method scoping
- parameter passing
- package
custom java class
- modifying string, int, calendar, array, we can do it by using existing classes
- when we found another unique entity, we need to custom a class
using THIS keyword
- THIS adalah reference ke variable yang ada pada object itu sendiri
- dengan kata lain THIS adalah self referencing pointer
THIS using example
public class Rectangle{
private double length;
private double width;
public Rectangle(){
}
public void setLength(double length){
this.length = length;
}
public void setWidth(double width){
this.width = width;
}
}
examine this following snippet
- Rectangle kotak = new Rectangle()
- Rectangle is class method, owned by class, no need for isntantiation
- to use : Rectangle.ClassMethod();
- kotak = object, the instance method, owned by instance, need instantiation to use
- to use : kotak.InstanceMethod();
to make an instance method
- public static double compute(double length, double width)
- by adding static there
- need argument, it cant use a variable from the object
overloaded method
- is when methods with same name, is made with different signature
- example, the method print
- print can do it for many kinds
multiple constructors
- using THIS in a constructor means its calling other constructor
- ex :
public Rectangle(){
this(0., 0.);
}
public Rectangle(double length){
this(length, length);
}
public Rectangle(double length, double width){
setLength(length);
setWidth(width);
}
- the first Rectangle calls the second, and the second call the third
static initializers
- make the code get executed when a java class is loaded into the java system
- ex :
public class Point {
private static long x , y ;
static{
x = System.currentTimeMillis();
y = ++x;
}
public Point() {}
public static long getXY(){
return x+y;
}
}
- the code inside static is executed firstly after the class is loaded
overriding
- modify the behaviour of superclass method by rewriting it in the subclass
- tehe signature must exactly be the same
- ex :
public class Box extends Rectangle{
private double height;
public Box(){
this(0. , 0. , 0.);
}
public Box(double length, double width, double height){
super(length, width);
this.height = height;
}
public Box(double size){
this(size, size, size);
}
- Box is the subclass of Rectangle
- using THIS called the variable in that class
- using SUPER called the variable in its superclass
polymorphism
- where there are more classes extending from one including the superclass,
and most used as a bridge to get a value from its method
- ex :
public class TestShapes {
public static void main(String[] args) {
Rectangle square, dice, brick, rectangle;
square = new Rectangle(4. );
dice = new Box(3. );
brick = new Box(4. ,3. , 2.);
rectangle = new Rectangle(2. , 1.);
System.out.println(square);
System.out.println(dice);
System.out.println(brick);
System.out.println(rectangle);
}
}
Wednesday, March 17, 2010
Wednesday, March 10, 2010
5th week doin java
i'm getting used to it..
here's what i got today
selection statement and repetition
selection :
- using if statement
- using switch statement
- write boolean expression
- evaluate given boolean expression correctly
- nested if
- comparing objects
- selecting statement for given task
if syntax
- if ( )
else
relational operator
> more than
< less than
>= more than or equal to
<= less than or equal to
== equal to
!= not equal to
nested if
- if (comparing something){
if (comparing something){
do something
}
}
boolean operator
&& and
|| or
! not
boolean vars
- value : true / false
comparing object
- using ==
ex : String str1 = new String("java");
String str1 = new String("java");
str1 != str2
the location is different
ex : String str1 = new String("java");
str2 = str1
str1 == str2
the location is the same
- another way (to check only the value)
ex : if(str1.equals(str2)){
. . .
}
using switch case
- another branching statement with many possibilities
ex : switch ( N ){
case 1 : x = 30;
break;
case 2 : x = 40;
break;
case 3 : x = 50;
break;
default
trying something new
- using if statement and JOptionPane and also parseInt (converting String to Integer)
- here it is
IdealWeight.java
import javax.swing.JOptionPane;
public class IdealWeight {
public static void main(String[] args) {
int height,age;
double rec;
height = Integer.parseInt(JOptionPane.showInputDialog("input height : "));
age = Integer.parseInt(JOptionPane.showInputDialog("input age : "));
rec = (height - 100 + age / 10) * 0.9;
if(height > 140 && height < 230 && age > 0 && age < 100){
JOptionPane.showMessageDialog (null, "recom weight : " + rec );
}
else
JOptionPane.showMessageDialog (null, "error");
}
}
Repetition / looping
- using while - sentinel (have a certain statement to fulfill)
- using for - count controlled (have fixed times of loop)
commong looping mistakes
- off-by-one error
- make sure there is a statement which will terminate the loop
- make sure it loops just as many times as you wanted
while syntax
- while (statement){
do something
}
- while wont do the "something" unless the statement is fulfilled
do-while syntax
- do {
do something
}
- while(statement);
- do-while will do the "something" at least once before it evaluate the condition
for syntax
- for(starting condition; loop terminated condition; statement looped){
do something
}
a game to try on
not perfect, yet its enough for now
import javax.swing.JOptionPane;
public class HiLo {
private int secret,counter=0,guess;
public HiLo(){
secret = (int)(Math.random()*100);
JOptionPane.showMessageDialog(null, "I have a secret number, try and guess what it is");
while(counter<=6){
guess = Integer.parseInt(JOptionPane.showInputDialog("your guess : "));
if(guess < secret){
JOptionPane.showMessageDialog(null, "to low");
}
else if(guess > secret){
JOptionPane.showMessageDialog(null, "to high");
}
else if(guess == secret){
break;
}
counter++;
}
if(guess == secret){
JOptionPane.showMessageDialog(null, "my number is " + secret + ",you win");
}
else if(guess != secret)
JOptionPane.showMessageDialog(null, "my number is " + secret + ",you lost");
}
public static void main(String[] args) {
HiLo x = new HiLo();
}
}
thats all today...
here's what i got today
selection statement and repetition
selection :
- using if statement
- using switch statement
- write boolean expression
- evaluate given boolean expression correctly
- nested if
- comparing objects
- selecting statement for given task
if syntax
- if (
else
relational operator
> more than
< less than
>= more than or equal to
<= less than or equal to
== equal to
!= not equal to
nested if
- if (comparing something){
if (comparing something){
do something
}
}
boolean operator
&& and
|| or
! not
boolean vars
- value : true / false
comparing object
- using ==
ex : String str1 = new String("java");
String str1 = new String("java");
str1 != str2
the location is different
ex : String str1 = new String("java");
str2 = str1
str1 == str2
the location is the same
- another way (to check only the value)
ex : if(str1.equals(str2)){
. . .
}
using switch case
- another branching statement with many possibilities
ex : switch ( N ){
case 1 : x = 30;
break;
case 2 : x = 40;
break;
case 3 : x = 50;
break;
default
trying something new
- using if statement and JOptionPane and also parseInt (converting String to Integer)
- here it is
IdealWeight.java
import javax.swing.JOptionPane;
public class IdealWeight {
public static void main(String[] args) {
int height,age;
double rec;
height = Integer.parseInt(JOptionPane.showInputDialog("input height : "));
age = Integer.parseInt(JOptionPane.showInputDialog("input age : "));
rec = (height - 100 + age / 10) * 0.9;
if(height > 140 && height < 230 && age > 0 && age < 100){
JOptionPane.showMessageDialog (null, "recom weight : " + rec );
}
else
JOptionPane.showMessageDialog (null, "error");
}
}
Repetition / looping
- using while - sentinel (have a certain statement to fulfill)
- using for - count controlled (have fixed times of loop)
commong looping mistakes
- off-by-one error
- make sure there is a statement which will terminate the loop
- make sure it loops just as many times as you wanted
while syntax
- while (statement){
do something
}
- while wont do the "something" unless the statement is fulfilled
do-while syntax
- do {
do something
}
- while(statement);
- do-while will do the "something" at least once before it evaluate the condition
for syntax
- for(starting condition; loop terminated condition; statement looped){
do something
}
a game to try on
not perfect, yet its enough for now
import javax.swing.JOptionPane;
public class HiLo {
private int secret,counter=0,guess;
public HiLo(){
secret = (int)(Math.random()*100);
JOptionPane.showMessageDialog(null, "I have a secret number, try and guess what it is");
while(counter<=6){
guess = Integer.parseInt(JOptionPane.showInputDialog("your guess : "));
if(guess < secret){
JOptionPane.showMessageDialog(null, "to low");
}
else if(guess > secret){
JOptionPane.showMessageDialog(null, "to high");
}
else if(guess == secret){
break;
}
counter++;
}
if(guess == secret){
JOptionPane.showMessageDialog(null, "my number is " + secret + ",you win");
}
else if(guess != secret)
JOptionPane.showMessageDialog(null, "my number is " + secret + ",you lost");
}
public static void main(String[] args) {
HiLo x = new HiLo();
}
}
thats all today...
Wednesday, March 3, 2010
4th
so sleepy..
hv nothing to else say other than what i got at class...
here it is...
menu:
-argument/parameters
-passing obj to method
-constructor
-info hiding dan visibility modifiers
-local vars
info hiding
-the differences are between public or private
-with public,the info is accesible for other class
-with private it is not
-to access it,we need to use method
method
-syntax
( )
-arg# is the parameters which given the method a value to process
-there can be multiple parameters in a method by adding comma between each parameters
get the return value from method
-example
public double getBalance(){
return balance;
}
constructor
-is a special method thats executed when a new instance of class is created
-generated by compiler automatically when the file compiled
passing obj to method
-using object as a parameter in a method
local variables
-generated within a method,known only to that method
-example
public void swapBalance(Account vice,Account versa){
//tempBalance is a local var
double tempBalance=vice.getBalance();
vice.setBalance(versa.getBalance());
versa.setBalance(tempBalance);
thats all...
goin to sleep...
hv nothing to else say other than what i got at class...
here it is...
menu:
-argument/parameters
-passing obj to method
-constructor
-info hiding dan visibility modifiers
-local vars
info hiding
-the differences are between public or private
-with public,the info is accesible for other class
-with private it is not
-to access it,we need to use method
method
-syntax
-arg# is the parameters which given the method a value to process
-there can be multiple parameters in a method by adding comma between each parameters
get the return value from method
-example
public double getBalance(){
return balance;
}
constructor
-is a special method thats executed when a new instance of class is created
-generated by compiler automatically when the file compiled
passing obj to method
-using object as a parameter in a method
local variables
-generated within a method,known only to that method
-example
public void swapBalance(Account vice,Account versa){
//tempBalance is a local var
double tempBalance=vice.getBalance();
vice.setBalance(versa.getBalance());
versa.setBalance(tempBalance);
thats all...
goin to sleep...
Subscribe to:
Comments (Atom)
