Constructor Laboratory 7 2018/12/4.

Slides:



Advertisements
Similar presentations
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Advertisements

Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.
1 Lecture 4: Chapter 6 - Methods Outline Introduction Program Modules in Java Math -Class Methods Method Declarations Java API Packages Random-Number Generation.
Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using Arrays 7.5References and Reference Parameters.
JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
Internet Software Development Applets Paul J Krause.
JAPPLET.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions.
TCU CoSc Introduction to Programming (with Java) Variables and Methods.
4/15: Searching Arrays Look at BubbleSort.java Searching arrays: the linear search Searching arrays: the binary search.
11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
Chapter 3 Introduction to Java Applets 1 3 There is no main method in a Java Applet. A Java Applet can only run in a browser. An Applet is run.
Java I--Copyright © Tom Hunter. Chapter 3 Introduction to Java Applets.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
Review of CIS 120 Concepts: What you said you want….
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.3Sample.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Lesson 39: More wrapup on GUIs. 1.This presentation will focus on the decisions around software architecture and the decisions that will drive the code.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Dialog Boxes.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.
Variables and Methods Chapter 3 – Lecture Slides.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
Review Expressions and operators Iteration – while-loop – for-loop.
8/2: Recursion About Scoping.java Recursion Program of the Day.
8/8: Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays: the.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
Introducing, the JFrame Gives us a work area beside System.out.println.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
9/13: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
Java Applets.
Topic: Applets Course : JAVA PROGRAMMING Paper Code: ETCS-307
User Interface Programming In Java
Chapter 02 Data and Expressions.
“Form Ever Follows Function” Louis Henri Sullivan
Java Applets.
Java I.
UNIT-5.
Chapter 24 - Introduction to Java Applications and Applets
Java Applets.
Java Applets.
Java Applets.
Classes Lecture 7 from Chapter /1/11.
FINAL EXAM Final Exam Wednesday, Dec 14: 8: :00 AM (Frny G140)
Applet 2019/4/23.
Outline Character Strings Variables and Assignment
Enabling Application Delivery Via the Web
FINAL EXAM Final Exam Tuesday, May 3: 1:00 - 3:00 PM (Phys 112)
Presentation transcript:

Constructor Laboratory 7 2018/12/4

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

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; }

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;

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

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

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())

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

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

2018/12/42018/12/4

Return maximum 2018/12/42018/12/4

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 ));

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

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.

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

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

Star 2018/12/42018/12/4

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

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); }

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>

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);