150 likes | 282 Views
Week 6 : More About Objects and Methods. Outline. Methods calling Methods Static method, Static variable Overloading Constructor. Methods Calling Methods. public class CircleDemo { public static void main(String[] args) { Circle c = new Circle(); c.printCircleInfo(); } }.
E N D
Outline • Methods calling Methods • Static method, Static variable • Overloading • Constructor
Methods Calling Methods public class CircleDemo { public static void main(String[] args) { Circle c = new Circle(); c.printCircleInfo(); } } public class Circle { private static double PI = 3.141592; private double radius = 3.0; public double calCircumference() { return 2 * PI * radius; } public double calArea() { return PI * radius * radius; } public void printCircleInfo() { System.out.println("The radius of this triangle is " + radius); System.out.println("The circumference of this circle is " + calCircumference()); System.out.println("The area of this circle is " + calArea() ); } } c 300 PI 3.141592 300 radius = 3.0 public double calCircumference() { return 2*PI*radius; } public double calArea() { return PI * radius * radius; } public void printCircleInfo() { ...println("The ... " + radius); ...println("The ... " + calCircumference()); ...println("The ... " + + calArea() ); } The radius of this triangle is 3.0 The circumference of this circle is 18.849552000000003 The area of this circle is 28.274328000000004
Static Variable output count = 1 count_s = 0 count = 1 count_s = 0 count = 1 count_s = 2 count = 1 count_s = 2 count = 1 count_2 = 3 count = 1 count_2 = 3 public class StaticDemo { public int count = 0; public static int count_s = 0; public void writeOutput() { System.out.print(“count = “ + count); System.out.println(“ count_s = “ + count_s); } public static void main(String[ ] args) { StaticDemo obj1 = new StaticDemo(); StaticDemo obj2 = new StaticDemo(); obj1.count++; obj2.count++; obj1.writeOutput(); obj2.writeOutput(); obj1.count_s++; obj2.count_s++; obj1.writeOutput(); obj2.writeOutput(); StaticDemo.count_s++; obj1.writeOutput();obj2.writeOutput(); } }
Static Method • Sometimes need a method that does not require any kind of object. • computing the maximum of two integers • computing the square root of a number • converting a letter character from lowercase to uppercase • None of these methods has any obvious object to which it should belong. Such methods can be defined as static. • Static methods can be called without creating an object • Class_Name.Method_Name(...) • Object_Name.Method_Name(...) public class Circle { public static final double PI = 3.14159; public double radius; public static double area() { return (PI*radius*radius); } public double area1() { return (PI*radius*radius); } } public class Circle { public static final double PI = 3.14159; public static double area(double radius) { return (PI*radius*radius); } public static double circum(double radius) { return (PI*(radius+radius)); } } public class CircleDemo { public static void main(String[ ] args) { double r; Scanner keyboard = new Scanner(System.in); r = keyboard.nextDouble(); System.out.println(“area:”+Circle.area( r )); System.out.println(“Circumference:”+ Circle.circum( r )); } } Instance variables except static variables cannot be used in static methods.
Overloading average1 = 45.0 average2 = 2.0 average3 = b; • Defining several methods with the same name which differ from each other in terms of parameter list public class Statistician { public static double average(double first, double second) { return ((first + second)/2.0); } public static double average(double first, double second, double third) { return ((first + second + third)/3.0); } public static char average(char first, char second) { return (char)(((int)first + (int)second)/2); } public static void main(String[] args) { double average1 = Statistician.average(40.0, 50.0); double average2 = Statistician.average(1.0, 2.0, 3.0); char average3 = Statistician.average(‘a’, ‘c’); } }
Constructor • Constructor method is used for initializing instance variables when an object of a class is created. • Creating a new object class_name object_name = new class_name (parameter_list); • new class_name(parameter_list) • Create an object of a class_name type by calling one of constructors • Return a reference of a created object • Remarkable features • Constructors have the same name of the class. • Constructors don’t have any return type. (even void type) • Constructors often get to be overloaded.
Constructor Output Name:No name yet. Age:0 Weight:0.0 Name:Jane Doe Age:5 Weight:24.5 public PetRecord(String initName, int initAge, double weight) { name = initName; age = initAge; weight = initWeight; } publicvoid writeOutput() { System.out.println(“Name:”+name); System.out.println(“Age:”+age); System.out.println(“Weight:”+weight); } public static void main(String[ ] args) { PetRecord pet1 = new PetRecord(); PetRecord pet2 = new PetRecord(“Jane Doe”, 5, 24.5); pet1.writeOutput(); pet2.writeOutput(); } } public class PetRecord { private String name; private int age; private double weight; public PetRecord() { name = “No name yet.”; age = 0; weight = 0; } public PetRecord(String initName) { name = initName; age = 0; weight = 0; } } default constructor
Constructor • Returning reference
This • The keyword this stands for the calling object - the object that invokes the method. publicclass Point { privatedouble x, y; publicvoid setX(double newX) { x = newX; } .... } if the parameter name is changed to ‘x’ • publicvoid setX(double x) { • x = x; ??? • } • this.x = x;
public string toString(); public class Point { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public String toString() { return “(“ + x + “, ” + y + “)”; } public static void main(String[] args) { Point p = new Point(3, 4); System.out.println(p); } } public class Point { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } public static void main(String[] args) { Point p = new Point(3, 4); System.out.println( “(“ + p.getX() + ”,” + p.getY()+”)”); } } Which one do you think is better? (3,4) (3,4)
Practice • Student Management Program • Make a program that can manage students’ scores and rank their scores. • In student class, student ID has to be automatically generated. All objects of student class must not have the same ID. (hint : use static variable.)
Practice • Class Student • Instance variables • private static int staticID = 20080001; • private int studentID; • private String studentName; • private double midScore, finalScore; • Methods • public Student( ) • public Student(String studentName, double midScore, double finalScore) • public double getAvgScore() • Computes average score and return the value • avgScore = (midScore + finalScore) / 2 • public String getStudentName() • public double getMidScore(), getFinalScore() • public String toString() • (studentID) studentName : midScore , finalScore , avgScore • public static Student higherThan(Student s1, Student s2) • If a total score of s1 is higher than that of s2, return s1. • Else return s2.
Practice public class StudentTest { public static void main(String[] args) { Student s1 = new Student("Jane", 57.8, 79.5); Student s2 = new Student("Pole", 68.7, 77.0); Student s3 = new Student("John", 34.8, 56.0); Student s4 = new Student("Amian", 69.2, 60.7); System.out.println("----- Student LIST -----"); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); System.out.println(); Student max_st = s1; max_st = Student.higherThan(max_st, s2); max_st = Student.higherThan(max_st, s3); max_st = Student.higherThan(max_st, s4); System.out.println("Highest : " + max_st); Student min_st = s1; if(min_st == Student.higherThan(min_st, s2)) min_st = s2; if(min_st == Student.higherThan(min_st, s3)) min_st = s3; if(min_st == Student.higherThan(min_st, s4)) min_st = s4; System.out.println("Lowest : " + min_st); } }
Practice • Result ----- Student LIST ----- (20080001) Jane : 57.8 , 79.5 , 68.65 (20080002) Pole : 68.7 , 77.0 , 72.85 (20080003) John : 34.8 , 56.0 , 45.4 (20080004) Amian : 69.2 , 60.7 , 64.95 Highest : (20080002) Pole : 68.7 , 77.0 , 72.85 Lowest : (20080003) John : 34.8 , 56.0 , 45.4