Download This program shows how to use class methods. Please note, no

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
This program shows how to use class methods. Please note, no object is created.
Figure 7.6
// Java0706.java
// The problem of Java0705.java is now fixed. It is possible to declare
// multiple classes in one program. However, you must use the dot.method
// syntax to call any of the <Address> class methods.
public class Java0706
{
public static void main(String args[])
{
System.out.println("\nJAVA0706.JAVA\n");
Address.fullName();
Address.street();
Address.cityStateZip();
System.out.println();
}
}
class Address
{
public static void fullName()
{
System.out.println("Kathy Smith");
}
public static void street()
{
System.out.println("7003 Orleans Court");
}
public static void cityStateZip()
{
System.out.println("Kensington, Md. 20795");
}
}
Notice that all the methods are static – this means they will be used as class methods.
This program demonstrates parameters.
Figure 7.13
// Java0712.java
// This program demonstrates passing two parameters to a method.
// The <showArea> method is called twice. In this case reversing
// the sequence of the parameters is not a problem.
public class Java0712
{
public static void main(String args[])
{
System.out.println("\nJAVA0712.JAVA\n");
int width = 100;
int height = 50;
showArea(width,height);
showArea(height, width);
System.out.println();
}
public static void showArea(int w, int h )
{
System.out.println();
int area = w * h;
System.out.println("The rectangle area is " + area);
System.out.println();
}
}
Now, this program uses void methods. Also, notice that this program is using Class
methods because all of the methods in the Calc class are of the static form. You cannot
make an object of the Calc class.
Figure 7.17
// Java0716.java
// This program demonstrates how to create a four-function <Calc> class with void methods.
public class Java0716
{
public static void main(String args[])
{
System.out.println("\nJAVA0716.JAVA\n");
int number1 = 1000;
int number2 = 100;
Calc.add(number1,number2);
Calc.sub(number1,number2);
Calc.mul(number1,number2);
Calc.div(number1,number2);
System.out.println();
}
}
class Calc
{
public static void add(int n1, int n2)
{
int result = n1 + n2;
System.out.println(n1 + " + " + n2 + " = " + result);
}
public static void sub(int n1, int n2)
{
int result = n1 - n2;
System.out.println(n1 + " - " + n2 + " = " + result);
}
public static void mul(int n1, int n2)
{
int result = n1 * n2;
System.out.println(n1 + " * " + n2 + " = " + result);
}
public static void div(int n1, int n2)
{
int result = n1 / n2;
System.out.println(n1 + " / " + n2 + " = " + result);
}
}
Here is an example of a method that is a return method.
Figure 7.18
// Java0717.java
// This program demonstrates the difference between a
// void <Sum1> method and a return <Sum2> method.
// There are two differences.
// Void and return methods are declared differently.
// Void and return methods are also called differently.
public class Java0717
{
public static void main(String args[])
{
System.out.println("\nJAVA0717.JAVA\n");
int nbr1 = 1000;
int nbr2 = 100;
sum1(nbr1,nbr2);
System.out.println(nbr1 + " + " + nbr2 + " = " + sum2(nbr1,nbr2));
System.out.println();
}
public static void sum1(int n1, int n2)
{
int sum = n1 + n2;
System.out.println(n1 + " + " + n2 + " = " + sum);
}
public static int sum2(int n1, int n2)
{
int sum = n1 + n2;
return sum;
}
}
Objects
This program shows that you can only have one Piggy because all of the methods are in a
static state – pun intended!
Figure 8.1
// Java0801.java
// This program introduces the Piggy class, which will be used
// to demonstrate a variety of Object Oriented Programming features.
// This program uses static class variables and static class methods.
// Every Piggy method is accessed using the Piggy class identifier.
public class Java0801
{
public static void main(String args[])
{
System.out.println("\nJAVA0801.JAVA\n");
Piggy.initData();
Piggy.showData();
Piggy.addData(1200);
Piggy.showData();
System.out.println();
}
}
class Piggy
{
static double savings;
public static void initData()
{
savings = 0;
}
public static void addData(double s)
{
savings += s;
}
public static void showData()
{
System.out.println("Savings: " + savings);
}
}
If Piggy is a class, and the methods in the class are static, it means that there cannot be
multiple critters of the Piggy class lurking about. There is one Piggy class, and that is it.
This program creates a new object of the Piggy class.
Notice that the keyword ‘static’ is now gone. It will prevent you from creating an object!
Figure 8.3
// Java0803.java
// In this program a tom object of the Piggy class is instantiated.
// The program compiles now because the methods of the Piggy
// class are treated like "object" methods.
public class Java0803
{
public static void main(String args[])
{
System.out.println("\nJAVA0803.JAVA\n");
Piggy tom = new Piggy();
tom.initData();
tom.showData();
tom.addData(1200);
tom.showData();
System.out.println();
}
}
class Piggy
{
double savings;
public void initData()
{
savings = 0;
}
public void addData(double s)
{
savings += s;
}
public void showData()
{
System.out.println("Savings: " + savings);
}
}
This program shows the creation of two constructors that automatically run. One is a
default constructor the other is a parameter constructor.
Figure 8.7
// Java0806.java
// This program demonstrates that constructors can be overloaded.
// It is possible to supply information, using parameters, to the
// new object at the instance that it is constructed.
public class Java0806
{
public static void main(String args[])
{
System.out.println("\nJAVA0806.JAVA\n");
Piggy tom = new Piggy();
tom.showData();
Piggy sue = new Piggy("Sue",1800);
sue.showData();
System.out.println();
}
}
class Piggy
{
double savings;
String name;
public Piggy()
{
System.out.println("No parameter constructor");
savings = 0;
name = "";
}
public Piggy(String n,double s)
{
System.out.println("Parameter constructor");
name = n;
savings = s;
}
public void showData()
{
System.out.println("Name: " + name);
System.out.println("Savings: " + savings);
}
}