aalborg media lab 17-jun-15 software design lecture 4 “ using classes & objects”

31
Aalborg Media Lab Mar 25, 2022 Software Design Lecture 4 “ Using Classes & Objects”

Post on 20-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

Software Design

Lecture 4

“ Using Classes & Objects”

Page 2: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

Using Classes and Objects

• New Homepage:

– http://home1.stofanet.dk/software-design/[email protected]

• Chapter 3

– predefined objects (Strings)

– creating and using objects

– class libraries (ex. Math class)

Page 3: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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

Page 4: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

Objects and Classes

Bank Account

A class(the concept)

John’s Bank AccountBalance: $5,257

An object(the realization)

Bill’s Bank AccountBalance: $1,245,069

Mary’s Bank AccountBalance: $16,833

Multiple objectsfrom the same class

Page 5: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

Inheritance• Classes can be organized into inheritance

hierarchies

Bank Account

Account

Charge Account

Savings Account

Checking Account

Page 6: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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)

Page 7: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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.

Page 8: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

Creating Objects

-

-

num

name

int num;String name;

42num

name

num = 42;name = new String(“Mike”);

Mike

Page 9: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

Creating Objects• Generally, we use the new operator to create an object

title = new String (“Mike");

This calls the String constructor, which isa special method that sets up the object

• Creating an object is called instantiation• An object is an instance of a particular class

Page 10: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

Aliasesname1 = new String(“Mike”); name2 = new String(“Robert”);

name1 Mike

name2 Robert

name1 = name 2;

name1

name2 Robert

Page 11: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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

Page 12: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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)

Page 13: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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

Page 14: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

mutation1

“Change is inevitable, except from vending machines”

mutation2

“CHANGE IS INEVITABLE … MACHINES”

mutation3

“CHANGX IS INXVITABLX … MACHINXS.”

mutation4

“NGX IS INXVITABLX, XXCXPT F”

Page 15: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

Packages

• The classes of the Java standard class library are organized into packages

Package

java.langjava.appletjava.awtjavax.swingjava.netjava.utiljavax.xml.parsers

Purpose

General supportCreating applets for the webGraphics and graphical user interfacesAdditional graphics capabilities and componentsNetwork communicationUtilitiesXML document processing

Page 16: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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.*;

Page 17: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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)

Page 18: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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

Page 19: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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)

Page 20: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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

Page 21: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

Wrapper Classes• There is a wrapper class in the java.lang

package for each primitive type:

byte Byte

short Short

int Integer

long Long

float Float

double Double

Page 22: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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

Page 23: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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

Page 24: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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.

Page 25: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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

Page 26: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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

Page 27: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

Nested Panels• Nested Panels are used to create an intricate

containment hierarchy of components.

–JFrame •JPanel

–Label–Label

–JFrame •JPanel

–JPanel»Label

–JPanel»Label

Page 28: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

Listing 3.8import 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);

Page 29: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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

Page 30: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

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

Page 31: Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”

Aalborg Media LabApr 18, 2023

Exercises

• 3.1 – 3.5

DO NOT READ INPUT FROM USERUSE CONSTANTS!!

final int x1 = 14;final int y1 = 15;