Chapter 3 class note

1.     Create a class that calls a method named "methodWithTwoBlock" . Inside this method, there are two blocks scopes.

public class Block

{

public static void main(String[] args)

{

methodWithTwoBlocks();

}

public static void methodWithTwoBlocks()

{

int aNumber = 22;

System.out.println("Number is" + aNumber);

{

int anotherNumber = 99;

System.out.println("Number is" + aNumber);

System.out.println("Another" + anotherNumber);

}

System.out.println("aNumber is " + aNumber);

}

}

Overrides - If you declare a variable within a class, and use the same variable name within a method of the class, then the viable used inside the method takes precedence, or overrides.

public class Block

{

public static void main(String[] args)

{

int aNumber = 11; // aNumber is first time defined

methodWithTwoBlocks();

}

public static void methodWithTwoBlocks()

{

int aNumber = 22; // aNumber is overrided.

System.out.println("Number is" + aNumber);

{

int anotherNumber = 99;

System.out.println("Number is" + aNumber);

System.out.println("Another" + anotherNumber);

}

System.out.println("aNumber is " + aNumber);

}

}

result of the above program:

C:\Java2001>java Block

Number is22

Number is22

Another99

aNumber is 22

Class practice:

Use the example above, create another program that defines x = 5; and then write a method named "another" that defines x = 10

Print the x from both the main method and from the "another" method.

Public class num

public static void main(String[] args)

{

int x = 5; // the first time defined the value of x

System.out.println(" x= " + x);

Another();

System.out.println(" x= " + x);

}

public static void Another()

{

int x = 10; // override the value that was defined outside the scope.

System.out.println(" x= " + x);

 

}

}

Overloading

 

Several methods use a same method name. These methods have different types. For example, the following four methods have the same name but have different types of arguments.

 

Public static void add(int n1, int n2)

Public static void add(int n1, double n2)

Public static void add(double n1, int n2)

Public static void add(double n1, double n2)

public class Overload

{

public static void main (String[] args)

{

double balance = 1000;

double rate = .05;

int rate1 = 5;

money(balance,rate);

money(balance, rate1);

 

}

public static void money(double b, double r)

{

double interest;

interest = b * r;

System.out.println("Interest =" + interest);

}

public static void money(double b, int r)

{

double interest;

double percent;

 

percent = r/100.;

interest = b * percent;

System.out.println("Interest =" + interest);

}

 

}

  Overloading Constructors-

You can create a constructor that takes an argument, one argument or different data type arguments.

public class Employee

{

private int siteNumber;

private int empNumber;

private double memberFee;

Employee( ) // overload constructor

{

siteNumber=1234;

empNumber = 9999;

memberFee = 100;

}

Employee(int sN, int eN, double mF) // overload constructor

{

siteNumber = sN;

empNumber = eN;

memberFee = mF;

}

public void display( )

{

System.out.println(" the siteNumber = " + siteNumber);

System.out.println(" the empNumber = " + empNumber);

System.out.println(" the memberFee is = " + memberFee);

}

}

public class testE

{

public static void main(String[] args)

{

Employee aPerson = new Employee();

Employee bPerson = new Employee(0,0,0);

 

aPerson.display( );

bPerson.display( );

}

}

the result from the above question:

C:\Java2001>java testE

the siteNumber = 1234

the empNumber = 9999

the memberFee is = 100.0

the siteNumber = 0

the empNumber = 0

the memberFee is = 0.0

 

class practice:

Create a class named construct. In the construct class, create method to initialize a person’s name, age, and income. Include two constructors in this class.

Create a second class called testCon that call the construct class.

public class construct

{

private int age;

private String name;

private double income;

 

construct( )

{

age = 100;

name = "Bush";

income = 10;

}

construct( int a)

{

age = a;

name = "Bush";

income = 10;

 

}

construct( int a, String n)

{

age = a;

name = n;

income = 10;

}

construct( int a, String n, double i)

{

age = a;

name = n;

income = i;

 

}

public void setAge(int a)

{

age = a;

}

public int getAge()

{

return age;

}

public void setName(String n)

{

name = n;

}

public String getName()

{

return name;

}

public void setIncome(double inc)

{

income = inc;

}

public double getIncome()

{

return income;

}

testCon:

public class testCon

{

public static void main(String[] args)

{

construct aCon =new construct(18);

construct bCon = new construct(19,"Shin");

construct cCon = new construct(20,"Shin",1000 );

construct dCon = new construct( );

 

// aCon

System.out.println("acon");

System.out.println("age = " + aCon.getAge());

System.out.println("name = " + aCon.getName());

System.out.println("Income = " + aCon.getIncome());

// bCon

System.out.println("bcon");

System.out.println("age = " + bCon.getAge());

System.out.println("name = " + bCon.getName());

System.out.println("Income = " +bCon.getIncome());

// cCon

System.out.println("ccon");

System.out.println("age = " + cCon.getAge());

System.out.println("name = " + cCon.getName());

System.out.println("Income = " + cCon.getIncome());

//dCon

System.out.println("dcon");

System.out.println("age = " + dCon.getAge());

System.out.println("name= " + dCon.getName());

System.out.println("Income = " + dCon.getIncome());

}

}

section B - using method

1. this pointer - The implicitly reference( this) to get the private variables.

public class Employee

{

private int siteNumber;

private int empNumber;

private double memberFee;

Employee( )

{

siteNumber=1234;

empNumber = 9999;

memberFee = 100;

}

Employee(int sN, int eN, double mF)

{

siteNumber = sN;

empNumber = eN;

memberFee = mF;

}

public void display( )

{

System.out.println(" the siteNumber = " + this.siteNumber);

System.out.println(" the empNumber = " + this.empNumber);

System.out.println(" the memberFee is = " + this.memberFee);

}

}

Same meaning as above:

public void display( )

{

System.out.println(" the siteNumber = " + Employee.siteNumber);

System.out.println(" the empNumber = " + Employee.empNumber);

System.out.println(" the memberFee is = " + Employee.memberFee);

}

or you don’t need to refer to this because this is referenced automatically by the complier.

public void display( )

{

System.out.println(" the siteNumber = " + siteNumber);

System.out.println(" the empNumber = " + empNumber);

System.out.println(" the memberFee is = " + memberFee);

}

2. final - You cannot change the value of a symbolic after declaring it; any attempt to do so will result in a compiler error. You cannot change the value for a final type.

3. math functions.

4. Using prewritten imported methods

If you want to include the date, you must import java.util.Date.

import java.util.*;

public class DemoDate

{

public static void main(String[] args)

{

Date startTime = new Date( );

Date Today = new Date(101,8,29); // 9-29-2001

System.out.println("The current date is " + startTime);

System.out.println("Today is " + Today);

}

}

Note – the first argument is based on year minus 1900.

The second argument is a value of month minus 1.