Download presentation
Presentation is loading. Please wait.
1
Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”
2
Aalborg Media Lab 17-Jun-15 Using Classes and Objects New Homepage: –http://home1.stofanet.dk/software-design/ florian.pilz@stofanet.dk Chapter 3 –predefined objects (Strings) –creating and using objects –class libraries (ex. Math class)
3
Aalborg Media Lab 17-Jun-15 Introduction to Objects A class represents a concept, and an object represents the embodiment of a class An object represents something with which we can interact in a program A class can be used to create multiple objects An object provides a collection of services that we can tell it to perform for us The services are defined by methods in a class that defines the object
4
Aalborg Media Lab 17-Jun-15 Objects and Classes Bank Account A class (the concept) John’s Bank Account Balance: $5,257 An object (the realization) Bill’s Bank Account Balance: $1,245,069 Mary’s Bank Account Balance: $16,833 Multiple objects from the same class
5
Aalborg Media Lab 17-Jun-15 Inheritance Classes can be organized into inheritance hierarchies Bank Account Account Charge Account Savings Account Checking Account
6
Aalborg Media Lab 17-Jun-15 Using Objects (System.out) The System.out object represents a destination to which we can send output In the first program, we invoked the println method of the System.out object: System.out.println ("What is the frequency Kenneth?"); object methodinformation provided to the method (parameters)
7
Aalborg Media Lab 17-Jun-15 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference variable: String title; No object is created with this declaration. An object reference variable holds the address of an object and must itself be created separately.
8
Aalborg Media Lab 17-Jun-15 Creating Objects - - num name int num; String name; 42 num name num = 42; name = new String(“Mike”); Mike
9
Aalborg Media Lab 17-Jun-15 Creating Objects Generally, we use the new operator to create an object title = new String (“Mike"); This calls the String constructor, which is a special method that sets up the object Creating an object is called instantiation An object is an instance of a particular class
10
Aalborg Media Lab 17-Jun-15 Aliases name1 = new String(“Mike”); name2 = new String(“Robert”); name1 Mike name2 Robert name1 = name 2; name1 name2 Robert
11
Aalborg Media Lab 17-Jun-15 String Objects Because strings are so common, we don't have to use the new operator to create a String object title = "Java Software Solutions"; Once an object has been instantiated, we can use the dot operator to invoke its methods title.length()
12
Aalborg Media Lab 17-Jun-15 String Methods The String class has several methods that are useful for manipulating strings Many of the methods return a value, such as an integer or a new String object See the list of String methods in fig. 3.1 p. 119 (listing 3.1)
13
Aalborg Media Lab 17-Jun-15 public class StringMutation { public static void main (String[] args) { String phrase = "Change is inevitable"; String mutation1, mutation2, mutation3, mutation4; System.out.println ("Original string: \"" + phrase + "\""); System.out.println ("Length of string: " + phrase.length()); mutation1 = phrase.concat (", except from vending machines."); mutation2 = mutation1.toUpperCase(); mutation3 = mutation2.replace ('E', 'X'); mutation4 = mutation3.substring (3, 30); System.out.println ("Mutation #1: " + mutation1); System.out.println ("Mutation #2: " + mutation2); System.out.println ("Mutation #3: " + mutation3); System.out.println ("Mutation #4: " + mutation4); System.out.println ("Mutated length: " + mutation4.length()); }
14
Aalborg Media Lab 17-Jun-15 mutation1 “Change is inevitable, except from vending machines” mutation2 “CHANGE IS INEVITABLE … MACHINES” mutation3 “CHANGX IS INXVITABLX … MACHINXS.” mutation4 “NGX IS INXVITABLX, XXCXPT F”
15
Aalborg Media Lab 17-Jun-15 Packages The classes of the Java standard class library are organized into packages Package java.lang java.applet java.awt javax.swing java.net java.util javax.xml.parsers Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities and components Network communication Utilities XML document processing
16
Aalborg Media Lab 17-Jun-15 The import Declaration When you want to use a class from a package, you could use its fully qualified name or import it. java.util.Random import java.util.Random; import java.util.*;
17
Aalborg Media Lab 17-Jun-15 The import Declaration All classes of the java.lang package are imported automatically into all programs That's why we didn't have to import the System or String classes explicitly in earlier programs The Random class is part of the java.util package, which provides methods that generate pseudorandom numbers. (listing 3.2)
18
Aalborg Media Lab 17-Jun-15 public class RandomNumbers { public static void main (String[] args) { Random generator = new Random(); int num1; float num2; num1 = generator.nextInt(); System.out.println ("A random integer: " + num1); num1 = generator.nextInt(10); System.out.println ("From 0 to 9: " + num1); num1 = generator.nextInt(10) + 1; System.out.println ("From 1 to 10: " + num1); }
19
Aalborg Media Lab 17-Jun-15 Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods are called class methods or static methods The Math class contains many static methods, providing various mathematical functions. such as absolute value, trigonometry functions, square root, etc. value = Math.abs(total) + Math.pow(count,4)
20
Aalborg Media Lab 17-Jun-15 Wrapper Classes A wrapper class represents a particular primitive type Integer ageObj = new Integer (20); uses the Integer class to create an object which effectively represents the integer 20 as an object This is useful when a program requires an object instead of a primitive type
21
Aalborg Media Lab 17-Jun-15 Wrapper Classes There is a wrapper class in the java.lang package for each primitive type: byteByte shortShort intInteger longLong floatFloat doubleDouble
22
Aalborg Media Lab 17-Jun-15 Wrapper Classes Wrapper classes contain static methods that help manage the associated type –Integer class contains a method to convert an integer stored in a String to an int value: num = Integer.parseInt (str);
23
Aalborg Media Lab 17-Jun-15 Components and Containers Containers are special GUI components that hold and organize other components –Frame (is displayed as a separate window) –Panel (can only be displayed as part of another container) All visible elements are displayed in a frame’s content pane
24
Aalborg Media Lab 17-Jun-15 Components and Containers Components and Containers are classes and must be instantiated in order be used. These have methods the control of the visual- appearance / behavior (size, color, etc.). Every container is managed by a layout manager.
25
Aalborg Media Lab 17-Jun-15 import java.awt.*; import javax.swing.*; public class Authority { public static void main (String[] args) { JFrame frame = new JFrame ("Authority"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); JPanel primary = new JPanel(); primary.setBackground (Color.yellow); Listing 3.7
26
Aalborg Media Lab 17-Jun-15 Listing 3.7 primary.setPreferredSize (new Dimension(250, 75)); JLabel label1 = new JLabel ("Question authority,"); JLabel label2 = new JLabel ("but raise your hand first."); primary.add (label1); primary.add (label2); frame.getContentPane().add(primary); frame.pack(); frame.setVisible(true); }
27
Aalborg Media Lab 17-Jun-15 Nested Panels Nested Panels are used to create an intricate containment hierarchy of components. –JFrame JPanel –Label –JFrame JPanel –JPanel »Label –JPanel »Label
28
Aalborg Media Lab 17-Jun-15 Listing 3.8 import java.awt.*; import javax.swing.*; public class NestedPanels { public static void main (String[] args) { JFrame frame = new JFrame ("Nested Panels"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // Set up first subpanel JPanel subPanel1 = new JPanel(); subPanel1.setPreferredSize (new Dimension(150, 100)); subPanel1.setBackground (Color.green); JLabel label1 = new JLabel ("One"); subPanel1.add (label1);
29
Aalborg Media Lab 17-Jun-15 Listing 3.8 // Set up second subpanel JPanel subPanel2 = new JPanel(); subPanel2.setPreferredSize (new Dimension(150, 100)); subPanel2.setBackground (Color.red); JLabel label2 = new JLabel ("Two"); subPanel2.add (label2); // Set up primary panel JPanel primary = new JPanel(); primary.setBackground (Color.blue); primary.add (subPanel1); primary.add (subPanel2); frame.getContentPane().add(primary); frame.pack(); frame.setVisible(true); }
30
Aalborg Media Lab 17-Jun-15 Images with Labels Icons as JPEG or GIF –new Icon(“devil.gif”) A label can contain text, an image or both –new JLabel(String); –new JLabel(String, Icon, int horizontalAlignment) Labels may use orientation constants: –SwingConstants.CENTER
31
Aalborg Media Lab 17-Jun-15 Exercises 3.1 – 3.5 DO NOT READ INPUT FROM USER USE CONSTANTS!! final int x1 = 14; final int y1 = 15;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.