“Form Ever Follows Function” Louis Henri Sullivan

Slides:



Advertisements
Similar presentations
Graphic User Interfaces Layout Managers Event Handling.
Advertisements

CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
1 Lecture 4: Chapter 6 - Methods Outline Introduction Program Modules in Java Math -Class Methods Method Declarations Java API Packages Random-Number Generation.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Chapter 121 Window Interfaces Using Swing Chapter 12.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Introduction to Methods
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Chapter 6 - Methods Outline 6.1Introduction 6.3 Math Class Methods 6.4Methods 6.5Method Definitions 6.6Java API Packages 6.7Random Number Generation 6.8Example:
Layout Managers Arranges and lays out the GUI components on a container.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1Introduction 6.2Program Modules in Java 6.3 Math Class Methods 6.4Methods.
Review for Nested loops & Math class methods & User defined methods.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
 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.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
A First Look at GUI Applications
Java GUI.
Methods Chapter 6.
Java Programming: From Problem Analysis to Program Design,
Introduction to Methods
Ellen Walker Hiram College
Message, Input, Confirm, and Specialized Dialogs
Chapter 13: Advanced GUIs and Graphics
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 Methods: A Deeper Look
MSIS 655 Advanced Business Applications Programming
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
CS360 Client/Server Programming Using Java
In this class, we will cover:
CiS 260: App Dev I Chapter 6: GUI and OOD.
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

“Form Ever Follows Function” Louis Henri Sullivan Methods “Form Ever Follows Function” Louis Henri Sullivan CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Purpose of Methods Why do you think it is important for us to use methods? What things do we need to know about methods before we can use them? Modularity, divide and conquer, write things only once, software reusability, debugging and maintenance, information hiding CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Modularity Modularity is achieved in Java in two ways Classes Methods CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Java API Math class One of the most useful Java classes Math.sqrt( 900.0 ); Math.PI Math.E Point out that Math starts with a capital letter, but sqrt does not. CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Example Write a Java application that will display the squares of the first ten integers starting from 1. The example here should include the three different ways of calling methods CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Squares Program public class SquareIntegers { public static void main( String args[] ) JTextArea outputArea = new JTextArea( ); int result; String output = ""; for( int i=1; i<=10; i++) result = square( i ); output += "The square of " + i + " is " + result +"\n"; } Which of these are methods? Where are the method calls? How are the method calls different? CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Squares Program outputArea.setText( output ); JOptionPane.showMessageDialog( null, outputArea, "Squares", JOptionPane.INFORMATION_MESSAGE ); }//end main public static int square( int y ) { return y*y; }//end square }//end class Static methods can only call other static methods on their own CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Calling Methods There are several ways to call methods Just use the method name square( i ) Use an objects followed by . and method name outputArea.setText( output ); Use the class name to qualify the method JOptionPane.showMessageDialog( … ) Only works with static methods What does static mean? CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Properties of Methods Method header Return_value Method_name( argument1, argument2 ) Method body { declarations, statements } A method cannot be declared inside another method CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Argument Promotion Applies to Java primitive types Primitive data types can be promoted to other data types Similar to type casting in C++ What problems can you see with this? CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Promotion Rules double none float long float or double int long, float or double char int, long, float or double short byte short, int, long, float or double boolean CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Type Casting What would happen if we tried to force a promotion from a higher data type (double) to a lower data type (int)? For example, what would happen if we added the following statements to our squares program: result = square( 9.5 ); output += result; CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Type Casting The compiler throws an error: SquareIntegers.java:18: square(int) in SquareIntegers cannot be applied to (double) result = square(9.5); ^ We must force the type casting result = square( (int) 9.8 ); output += "The square of 9.8 is " + result + "\n"; CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java GUI A GUI presents a user-friendly mechanism for interacting with a program GUI’s are built from GUI components Controls or widgets Examples of widgets: JTextArea JTextField JLabel JButton Start talking about GUI (layout managers and gui components) Constants Action listeners CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java GUI So far we have Displayed messages in dialog boxes JOptionPane.showMessageDialog( null, “Message”, “Title”, JOptionPane.INFORMATION_MESSAGE ); Receive input from the user through a dialog box JOptionPane.showInputDialog( “Enter Number” ); Added a widget to a dialog box JOptionPane.showMessageDialog( null, widget, “title”, JOptionPane.INFORMATION_MESSAGE ); CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java GUI GUI widgets can be placed in their own window Not using dialog boxes To do this in applets, widgets need to be placed on to the onscreen display area Called the content pane The location where the widgets get placed depends on the layout manager CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Layout Managers Programmer can specify where the widget is going to appear on the window using the X and Y coordinates Use layout manager: FlowLayout Widgets added sequentially, left to right BorderLayout Widgets are added to NORTH, SOUTH, EAST, WEST or CENTER GridLayout Arranges components into rows and columns CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java FlowLayout CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java BorderLayout CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java GridLayout 2 rows, 3 columns CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Constants Constants are declared in Java using the keyword final final int WON = 0, LOST = 1, CONTINUE = 2; CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Event Handling To handle user interaction with the GUI, Java programs must contain the necessary event handling code Event handling is performed by Implementing the interface ActionListener public class BorderLayoutTest extends JApplet implements ActionListener { } Adding the method ActionPerformed public void actionPerformed( ActionEvent actionEvent ) { } CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Event Handling We need to also import the necessary classes import java.awt.event.*; Add an action listener to each widget that accepts an event from the user button1.addActionListener( this ); CS360 Client/Server Programming Using Java 6/8/2018

Event Handling Program CS360 Client/Server Programming Using Java 6/8/2018

Multiplication Program Write a program that will assist children in learning multiplication of single digit numbers. Your program should display a text label asking the children the question, a text field for the user to enter the result, a button to submit the answer and a label displaying if the answer was correct or incorrect If the answer was incorrect, the user should be asked the same question again, otherwise the numbers change CS360 Client/Server Programming Using Java 6/8/2018

Multiplication Program The message displayed after the user has submitted an answer should randomly choose from a list of different messages, so that the user doesn’t get bored CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Fields Fields are variables that have been declared inside a class but outside of any method These fields are accessible to all methods Called instance variables in other programming languages CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Scope of Declarations The scope of a parameter declaration is the body of the method in which the declaration appears The scope of a local variable declaration is from the point at which the declaration appears in the block to the end of that block The scope of a local variable declaration that appears in the initialisation of a for statement’s header is the body of the for statement CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Scope of Declarations The scope of a method or field of a class is the entire body of the class If a local variable or parameter in a method has the same name as a field, the field is “hidden” until the block terminates executing This is called shadowing CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java What is the Output? CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Method Overloading Several methods of the same name can be declared in the same class as long as they have different parameters This is called method overloading Example: Public int square( int x ) { } Public double square( double x ) { } CS360 Client/Server Programming Using Java 6/8/2018

CS360 Client/Server Programming Using Java Summary Today we covered: Random numbers GUI components GUI layout managers Event handling Scope of declaration Method overloading Reading: Today we covered Chapter 6 up till 6.12 Remainder of chapter 6 covers recursion (not required) So far we have completed chapters 1 through 7 Next time we will start on object based programming (chapter 8) CS360 Client/Server Programming Using Java 6/8/2018