Chapter 2 – Using Methods, Classes, and Objects

Section A - Programming Using Methods

A method is a series of statements that carry out some task.

  1. Creating Methods with no arguments.

Example:  The first program calls greeting method to perform the greeting task.

public class ch2_1

{

                                public static void main (String[] args)

                {

                                System.out.println(" use another program");

                                greeting( );

                }

                public static void greeting( )

                {

                                System.out.println(" Hi, how are you!");

                                System.out.println(" My name is Shin Liu");

                }

}

Example 2:   The SetUpSite class calls the StatementOfPhilosophy to print the philosophy statements

public class SetUpSite

{

                                public static void main (String[] args)

                {

                                statementOfPhilosophy( );

                }

                public static void statementOfPhilosophy( )

                {

                                System.out.println(" Event Handlers Incorporated is");

                                System.out.println(" dedicated to making your event");

                                System.out.println("a most memorable one.");

                }

}

Example 3 :  You can call a method from another class.

public class Test

{

                public static void main(String[] args)

                {

                System.out.println

                  ("Calling method from another class:");

                SetUpSite.statementOfPhilosophy( );

                }

}

 

  1. Methods that require a single argument

 

Example 1:  The demo class calls the prdictRaise class to calculate the new salary.

public class demo

{

                public static void main(String[] args)

                {

                double salary = 200;

                System.out.println("Demonstrating some raises");

                predictRaise(400);

                predictRaise(salary);

                }

public static void predictRaise(double money)

{

                double newAmount;

                newAmount = money * 1.10;

                System.out.println("With raise salary is " + newAmount);

}

}

 

Example 2 :  The testdemo class calls the predictRaise method in the demo class.

public class testdemo

{

                public static void main(String[] args)

                {

                  double salary= 300;

                  System.out.println("call the predit method from demo class");

                  demo.predictRaise(salary);

                }

}

  1. Methods that require multiple arguments

 

Example 1:  demo1 class calles the predictRaise method , and passes two arguments to the method.

public class demo1

{

                public static void main(String[] args)

                {

                double salary = 200;

                double rate = .15;

                System.out.println("Demonstrating some raises");

                predictRaise(400, rate);

                predictRaise(salary,rate);

                }

public static void predictRaise(double money, double rate)

{

                double newAmount;

                newAmount = money * rate;

                System.out.println("With raise salary is " + newAmount);

}

}

Example 2:  The testdemo1 class calls the preditRaise method , that is in the demo class

public class testdemo1

{

                public static void main(String[] args)

                {

                  double salary= 300;

                  double rate = .20;

                  System.out.println("call the predit method from demo class");

                  demo1.predictRaise(salary,rate);

                }

}

  1. Methods that returns  values

 

Example 1 :  The demo2 class calles passes two arguments to predictRaise method and receives a number from the method.

public class demo2

{

                public static void main(String[] args)

                {

                double salary = 200;

                double rate = .15;

                double newSalary;

                System.out.println("Demonstrating some raises");

                newSalary = predictRaise(salary, rate);

                System.out.println("The new salary is " + newSalary);

                }

public static double predictRaise(double money, double rate)

{

                double newAmount;

                newAmount = money *( 1 +  rate);

                return newAmount;

 

}

}

 

Section B  - Using Classes

1. Use a method to get values from the user.

Example 1:  The fraction class is the blue print. 

public class fraction

{

                private int numerator;

                private int denominator;

 

                public int getnum( )

                {

                  return numerator;

                }

                public void setnumber(int n, int d)

                {

                   numerator = n;

                   denominator = d;

                }

                public int getden()

                {

                return denominator;

                }

                public void print( )

                {

                  System.out.println("the fraction is " + numerator + " / " + denominator);

                }

 

               

                public static void main(String[] args)

                {

                  fraction f1= new fraction();

                  f1.setnumber( 3,8);

                  f1.print( );

                }

 

}

 

Example 2 :  You can instantiate an object that belongs to another class

public class fraction

{

                private int numerator;

                private int denominator;

 

                public int getnum( )

                {

                  return numerator;

                }

                public void setnumber(int n, int d)

                {

                   numerator = n;

                   denominator = d;

                }

                public int getden()

                {

                return denominator;

                }

                public void print( )

                {

                  System.out.println("the fraction is " + numerator + " / " + denominator);

                }

--- In another class ----

public class testfra

{

                public static void main(String[] args)

                {

                  fraction f1 = new fraction();

                  f1.setnumber( 3,8);

                  f1.print( );

                }

}

 

2. Using constructors to get values from the users.

 

Example 1:  The fraction1 class uses two constructors,- a default constructor and another constructor that assigns two values.

 

public class fraction1

{

                private int numerator;

                private int denominator;

 

                fraction1( )

                {

                   numerator = 1;

                   denominator = 1;

                }

                fraction1(int n, int d)

                {

                   numerator = n;

                   denominator = d;

                }

                public void print( )

                {

                  System.out.println("the fraction is " + numerator + " / " + denominator);

                }

 

               

}

--- in another class -----

public class testfra1

{

                public static void main(String[] args)

                {

                  fraction1 f2 = new fraction1( );

                  fraction1 f3 = new fraction1( 2,3);

 

                  f2.print();

                  f3.print();

                }

}