Download presentation
Presentation is loading. Please wait.
Published byBennett May Modified over 8 years ago
1
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects COMP 102 #8 2014T2
2
©Xiaoying Gao, Peter Andreae COMP102 8 :2 Menu Creating objects inside the program Admin A3 handed out A2 model solutions: do not distribute Awhina programme: teropuawhina@gmail.comteropuawhina@gmail.com Tutorials start this week
3
© Xiaoying Gao, Peter Andreae COMP102 7:3 Defining methods to return values public void askFourQuestions() { long time = this.measureTime("John Quay is the Prime Minister"); time = time + this.measureTime("11 x 13 = 143"); time = time + this.measureTime("Summer is warmer than Winter"); time = time + this.measureTime("Wellington has 1,000,000 people"); UI.printf("Average time = %d milliseconds\n", (time / 4)); } public long measureTime(String fact) { long startTime = System.currentTimeMillis(); String ans = UI.askString("Is it true that " + fact); long endTime = System.currentTimeMillis(); return (endTime - startTime) ; }
4
© Xiaoying Gao, Peter Andreae COMP102 8:4 Call a method objectName.methodName(arguments) Objects Predefined: UI, Math, System (they are in fact Class names) Default: this Create your own objects Arguments, data to use Check documentation (or your own code), to find out parameters Must match the type, order, number of parameters If the method returns a value Assign it to a variable of the same type (store it) Print it out (use it for output) Do nothing (throw it away)
5
© Xiaoying Gao, Peter Andreae COMP102 8:5 A complete class Constants Belong to class, e.g. Math.PI, Color.red Static methods Belong to class, e.g. UI.println(); Data fields: save data for the object Constructor: a special method for creating an object Non-static methods actions of the object Must call from a real object
6
© Xiaoying Gao, Peter Andreae COMP102 8:6 String Objects import ecs100.*; public class StringExample{ public void twoStrings(){ String s1 = "You only live once"; String s2 = new String("Thank God it is Friday"); char x = s1.charAt(0); UI.println(x); String s = s1.substring(5,15); UI.println(s); s = s2.substring(5,15); UI.println(s); }
7
© Xiaoying Gao, Peter Andreae COMP102 8:7 Color Objects import ecs100.*; import java.awt.Color; public class colorExample { public void colorMethod(){ Color c1 = new Color(200, 200, 50); UI.setColor(c1); UI.fillOval(50, 50, 50, 50); Color c2 = c1.brighter(); UI.setColor(c2); UI.fillOval(100, 50, 50, 50); String s = c2.toString(); UI.println(s); }
8
©Xiaoying Gao, Peter Andreae COMP102 8 :8 Creating Objects: new Color c1 = new Color(100, 50, 200); UI.setColor( new Color(255, 190, 0) ); Calling a constructor: new( a keyword) Color( the type of object to construct ) ( … )(arguments: specifying information needed to construct the new object, Check the documentation to find out ) This is an expression: it returns the object can put in variable can use in an enclosing expression or method call new〈 Class name 〉〈 arguments 〉 ()
9
© Xiaoying Gao, Peter Andreae COMP102 8:9 Scanner Example import ecs100.*; import java.util.Scanner; public class ScannerExample{ public void testScanner(){ Scanner sc = new Scanner("COMP102 2014 ECS VUW"); String course = sc.next(); int year = sc.nextInt(); UI.println(year); UI.println(course); UI.println(sc.nextLine()); }
10
© Xiaoying Gao, Peter Andreae COMP102 8:10 Call a method in the library Static ClassName.methodName(arguments) UI.println(“UI is in fact a class name”); Not Static Create an object new ClassName(arguments for constructor) Check documentation to find out ObjectName.methodName(arguments for the method)
11
© Xiaoying Gao, Peter Andreae COMP102 8:11 A class with fields and constructor import ecs100.*; public class BankAccount{ //simplified version of the exercise private String name; private double balance; public BankAccount(String n){ this.name = n; this.balance = 0.0; } public void deposit(double x){ this.balance = this.balance + x; } public void withdraw(double x){ this.balance = this.balance - x; } public void statement(){ UI.println(this.name + " $"+this.balance); } }
12
© Xiaoying Gao, Peter Andreae COMP102 8:12 Use the class to create new objects public class NewObjects{ public void createNewObj(){ BankAccount b1 = new BankAccount("Alice"); b1.deposit(100); BankAccount b2 = new BankAccount("Bob"); b2.deposit(200); b1.withdraw(50); b2.statement(); b1.statement(); }
13
© Xiaoying Gao, Peter Andreae COMP102 8:13 Call a method In the same class Normally use “this” In another class Static Call using class name Not Static Create objects, call from objects
14
© Xiaoying Gao, Peter Andreae COMP102 8:14 Why objects A new data type: group data together Group data and methods together Different objects co-exist The same class reused differently for different objects
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.