Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constructor Laboratory 7 2018/12/4.

Similar presentations


Presentation on theme: "Constructor Laboratory 7 2018/12/4."— Presentation transcript:

1 Constructor Laboratory 7 2018/12/4

2 Review Class definition Declaring Objects
2018/12/42018/12/4 Class definition Declaring Objects Assigning object reference variables Introducing methods Constructors

3 The General Form of a class
2018/12/42018/12/4 Class classname { Type instance-variable1; // Type methodname1(parameter-list) { //body of method } Type methodname2 (parameter-list { //body of method } …. Class person { Double height; Double weight; }

4 Creating the object box
2018/12/42018/12/4 Person theperson = new person(); Here, theperson will be an instance of person. To assign a value, use theperson.weight = 70; theperson.height = 170;

5 Declaring the object – page 135
2018/12/42018/12/4 Statement Effect Person theperson; theperson = new Person(); Null theperson Weight theperson Height theperson.height

6 Example of creating and assigning value
2018/12/42018/12/4

7 Introducing Methods Classes usually consist of two things:
2018/12/42018/12/4 Classes usually consist of two things: Instance variables (int x, y) and methods (pair())

8 Passing the values x = 1 and y = 2 will pass to Pair
2018/12/42018/12/4 x = 1 and y = 2 will pass to Pair

9 Returning a value – from myarea.area()
2018/12/42018/12/4

10 2018/12/42018/12/4

11 Return maximum 2018/12/42018/12/4

12 Call maximum (loop) double max =
2018/12/42018/12/4 double max = maximum(maximum( number1, number2, number3 ), maximum( number4, number5, number6 ), maximum( number7, number8, number9 ));

13 2018/12/42018/12/4 import java.awt.Container; import javax.swing.*;
public class MaxMin9Test extends JApplet { public void init() { String s1 = JOptionPane.showInputDialog("Enter first floating-point value" ); String s2 = JOptionPane.showInputDialog("Enter second floating-point value" ); String s3 = JOptionPane.showInputDialog("Enter third floating-point value" ); String s4 = JOptionPane.showInputDialog("Enter first floating-point value" ); ………. //fill in value double number1 = Double.parseDouble( s1 ); double number2 = Double.parseDouble( s2 ); double number3 = Double.parseDouble( s3 ); ………..// fill in value double max = maximum(maximum( number1, number2, number3 ), maximum( number4, number5, number6 ), maximum( number7, number8, number9 )); double min = ……….. Fill in value JTextArea outputArea = new JTextArea(); outputArea.setText( "number1: " + number1 + "\nnumber2: " + number2 + "\nnumber3: " + number3 + "\nnumber2: " + number4 + "\nnumber3: " + number5 + "\nnumber2: " + number6 + "\nnumber3: " + number7 + "\nnumber2: " + number8 + "\nnumber3: " + number9 + "\nmaximum is: " + max + "\nminimum is: " + min ); Container container = getContentPane(); container.add( outputArea ); } public double maximum( double x, double y, double z ) ……………// fill in value public double minimum( double x, double y, double z ) …………… // fill in value

14 Constructor 2018/12/42018/12/4 It is tedious to initialise all variables in a class each time an instance is created. Constructors are a special sort of method that initialise freshly minted objects. They are like static methods in that we don't need an existing object to use them, just new.

15 Example 2018/12/42018/12/4 the values are fixed

16 How to draw 2018/12/42018/12/4

17 Star 2018/12/42018/12/4

18 Background and position
2018/12/42018/12/4

19 Example 2018/12/42018/12/4 import java.awt.*;
import java.applet.Applet; import javax.swing.*; public class lab72 extends Applet { String s1 = JOptionPane.showInputDialog("Enter R" ); //enter the color code String s2 = JOptionPane.showInputDialog("Enter G" ); String s3 = JOptionPane.showInputDialog("Enter B" ); // convert user input to integer int i = Integer.parseInt(s1); int j = Integer.parseInt(s2); int k = Integer.parseInt(s3); public void paint( Graphics g ) { int[] xArr = {28,63,73,81,115,87,98,73,46,55}; int[] yArr = {55,55,24,55,55,80,108,89,108,80}; int[] x1Arr = {153,161,165,166,174,165,165,160,153,156}; int[] y1Arr = {20,22,16,24,27,30,39,32,33,27}; int[] x2Arr = {179,187,193,191,198,189,187,184,176,181}; int[] y2Arr = {66,69,65,73,78,79,85,79,78,74}; int[] x3Arr = {167,173,177,180,189,181,183,178,171,173}; int[] y3Arr = {118,118,110,118,118,125,132,128,133,125}; int[] x4Arr = {128,136,141,142,150,140,142,137,129,133}; int[] y4Arr = {158,161,154,162,165,168,177,171,173,166}; g.setColor(new Color(i, j, k)); g.fillRect(0, 0, 452, 300); g.setColor(Color.yellow); g.fillPolygon(xArr,yArr,10); g.fillPolygon(x1Arr,y1Arr,10); g.fillPolygon(x2Arr,y2Arr,10); g.fillPolygon(x3Arr,y3Arr,10); g.fillPolygon(x4Arr,y4Arr,10); }

20 HTML <html> <head>
2018/12/42018/12/4 <html> <head> <title>simple applet</title> </head> <body> <applet code = "lab72.class" width = 452 height = 302> </applet> </body> </html>

21 2018/12/42018/12/4 import java.awt.*; import java.applet.Applet;
import javax.swing.*; public class lab72 extends Applet { Color backGroundColor; /define public void init() { // fill in the three numbers here String firstNumber, secondNumber, thirdNumber; int number1, number2, number3; backGroundColor = new Color(…………..); // fill in the value } public void paint( Graphics g ) { int[] xArr = {28,63,73,81,115,87,98,73,46,55}; int[] yArr = {55,55,24,55,55,80,108,89,108,80}; int[] x1Arr = {153,161,165,166,174,165,165,160,153,156}; int[] y1Arr = {20,22,16,24,27,30,39,32,33,27}; int[] x2Arr = {179,187,193,191,198,189,187,184,176,181}; int[] y2Arr = {66,69,65,73,78,79,85,79,78,74}; int[] x3Arr = {167,173,177,180,189,181,183,178,171,173}; int[] y3Arr = {118,118,110,118,118,125,132,128,133,125}; int[] x4Arr = {128,136,141,142,150,140,142,137,129,133}; int[] y4Arr = {158,161,154,162,165,168,177,171,173,166}; g.setColor(…………….); //fill in the value by calling background g.fillRect(0, 0, 472, 322); // use same attributes of applet g.setColor(Color.red); g.fillRect(10, 10, 452, 302); // for a border of size 10 g.setColor(Color.yellow); g.fillPolygon(xArr,yArr,10); g.fillPolygon(x1Arr,y1Arr,10); g.fillPolygon(x2Arr,y2Arr,10); g.fillPolygon(x3Arr,y3Arr,10); g.fillPolygon(x4Arr,y4Arr,10);


Download ppt "Constructor Laboratory 7 2018/12/4."

Similar presentations


Ads by Google