tailieunhanh - Lecture Software construction - Lab 4: Classes and objects in java
In this chapter, the following content will be discussed: AWT (abstract window toolkit), swing, swing vs. AWT, containers and components, an applet is panel is a container, to create an applet, creating components, flowlayout, borderlayout with five buttons,. | Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java Contents Introduce to classes and objects in Java. Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes. Defining a Constructor: Example public class Counter { int CounterIndex; // Constructor public Counter() { CounterIndex = 0; } //Methods to update or access counter public void increase() { CounterIndex = CounterIndex + 1; } public void decrease() { CounterIndex = CounterIndex - 1; } int getCounterIndex() { return CounterIndex; } } Trace counter value at each statement and What is the output ? class MyClass { public static void main(String args[]) { Counter counter1 = new Counter(); (); int a = (); (); int b = (); if ( a > b ) (); else (); (()); } } A Counter | Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java Contents Introduce to classes and objects in Java. Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes. Defining a Constructor: Example public class Counter { int CounterIndex; // Constructor public Counter() { CounterIndex = 0; } //Methods to update or access counter public void increase() { CounterIndex = CounterIndex + 1; } public void decrease() { CounterIndex = CounterIndex - 1; } int getCounterIndex() { return CounterIndex; } } Trace counter value at each statement and What is the output ? class MyClass { public static void main(String args[]) { Counter counter1 = new Counter(); (); int a = (); (); int b = (); if ( a > b ) (); else (); (()); } } A Counter with User Supplied Initial Value ? This can be done by adding another constructor method to the class. public class Counter { int CounterIndex; // Constructor 1 public Counter() { CounterIndex = 0; } public Counter(int InitValue ) { CounterIndex = InitValue; } } // A New User Class: Utilising both constructors Counter counter1 = new Counter(); Counter counter2 = new Counter (10); Adding a Multiple-Parameters Constructor to our Circle Class public class Circle { public double x,y,r; // Constructor public Circle(double centreX, double centreY, double radius) { x = centreX; y = centreY; r = radius; } //Methods to return circumference and area public double circumference() { return 2**r; } public double area() { return * r * r; } } Constructors initialise Objects Recall the following OLD Code Segment: Circle aCircle = new Circle(); = ; // initialize center and radius = = ; aCircle = new Circle() ; At creation time the center and .
đang nạp các trang xem trước