CS360 Client/Server Programming Using Java

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Advertisements

Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Road Map Introduction to object oriented programming. Classes
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 12.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 – Object-Based Programming Part II 8.9 Composition 8.10 Garbage Collection 8.11 Static Class.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
CompSci 230 S Programming Techniques
Classes (Part 1) Lecture 3
3 Introduction to Classes and Objects.
C# Object-Oriented Program
“Form Ever Follows Function” Louis Henri Sullivan
Chapter 16: Classes and Data Abstraction
Chapter 7 Top-Down Development
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Road Map Introduction to object oriented programming. Classes
More about OOP and ADTs Classes
Object-Oriented Programming Using C++
Can perform actions and provide communication
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Can perform actions and provide communication
Classes, Objects, and Methods
Classes: A Deeper Look Outline
IFS410: Advanced Analysis and Design
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Classes and Data Abstraction
Package & Java Access Specifiers
More about OOP and ADTs Classes
Object-Oriented Programming Using C++ Second Edition
More On Enumeration Types
Learning Objectives Classes Constructors Principles of OOP
Week 3 Object-based Programming: Classes and Objects
CHAPTER 6 GENERAL-PURPOSE METHODS
Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look
Can perform actions and provide communication
Constructors, GUI’s(Using Swing) and ActionListner
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.
Chapter 8 Classes and Objects: A Deeper Look
Chapter 11 Inheritance and Encapsulation and Polymorphism
More C++ Classes Systems Programming.
Corresponds with Chapter 5
Chapter 5 Classes.
Presentation transcript:

CS360 Client/Server Programming Using Java Last Time We covered: Basic GUI components Layout managers Methods Random numbers Constants CS360 Client/Server Programming Using Java 4/4/2019

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 4/4/2019

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 4/4/2019

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 4/4/2019

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 4/4/2019

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 4/4/2019

CS360 Client/Server Programming Using Java What is the Output? CS360 Client/Server Programming Using Java 4/4/2019

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 4/4/2019

Object-Based Programming Classes are used to encapsulate data and methods Encapsulation enables information hiding although objects can communicate across interfaces Procedural programming is action oriented and the unit of programming is the function In Java, the unit of programming is the class CS360 Client/Server Programming Using Java 4/4/2019

Object-Based Programming Procedural programmers concentrate on the verbs Object oriented programmers concentrate on the nouns CS360 Client/Server Programming Using Java 4/4/2019

Abstract Data Types (ADT) ADTs hide their implementation from their clients Write a class time that will store the time in hours, minutes and seconds. The class will contain a constructor that sets all the data to zero, a setTime method to allow the client to set the time, and two methods to output the time in universal and decimal time. CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java public class Time1 extends Object { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59 public Time1() { setTime( 0, 0, 0 ); } CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java public void setTime( int h, int m, int s ) { hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); second = ( ( s >= 0 && s < 60 ) ? s : 0 ); } CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java public String toUniversalString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return twoDigits.format( hour ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ); } CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java public String toStandardString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ) + ( hour < 12 ? " AM" : " PM" ); } CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java Testing the Class To test the class that we just created, we need to create a new class that contains the main method. How are objects created from a class? How are methods called using the objects? CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java public class TimeTest1 { public static void main( String args[] ) { Time1 time = new Time1(); String output = "The initial universal time is: " + time.toUniversalString() + "\nThe initial standard time is: " + time.toStandardString(); time.setTime( 13, 27, 6 ); output += "\n\nUniversal time after setTime is: " + time.toUniversalString() + "\nStandard time after setTime is: " + time.toStandardString(); CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java time.setTime( 99, 99, 99 ); output += "\n\nAfter attempting invalid settings: " + "\nUniversal time: " + time.toUniversalString() + "\nStandard time: " + time.toStandardString(); JOptionPane.showMessageDialog( null, output, "Testing Class Time1", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java Information Hiding Information hiding is accomplished in object-oriented programming through the use of the private keyword Variables (fields or data members) and methods that have been declared as being private are not accessible to the clients outside of the class CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java Information Hiding Time1 time = new Time1( ); time.hour = 9; time.minute = 25; time.second = 0; These declarations are incorrect and the error will be caught by the compiler CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java The this Reference Every object can access a reference to itself with the keyword this The this reference can be used implicitly and explicitly to refer to the instance variables and methods of the object on which the method was called CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java The this Reference Can you think of an example where the this reference could be used? CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java Constructors What is a constructor? What is the syntax of constructors? CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java Constructors Recall from CS250 that classes can have many constructors In Java these are called overloaded constructors These constructors are diffrentiated by the number of arguments CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java Let’s review some of the GUI components that we talked about on Tuesday Write a Java applet that will use the time class that we have created in class. The applet should include text fields to allow the user to enter in the hour, minute and second, and a button that will move the time forward by one second CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java Time Program What GUI widgets do we need? Which of these widgets need to handle events from the user? What methods will we need in the program? CS360 Client/Server Programming Using Java 4/4/2019

CS360 Client/Server Programming Using Java Summary Today we covered: Scope of variables and methods Classes Constructors Information hiding The this reference From the book: We have completed chapters 1 through 7 We are half way through chapter 8, up to section 8.9 The assignment is due on Tuesday CS360 Client/Server Programming Using Java 4/4/2019