Data Types Reference Types

Similar documents
CST141 Thinking in Objects Page 1

9 Working with the Java Class Library

CT 229 Fundamentals of Java Syntax

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

ECE 122. Engineering Problem Solving with Java

Course PJL. Arithmetic Operations

Introduction to Programming Using Java (98-388)

Introduction to Java. Nihar Ranjan Roy.

Handout 3 cs180 - Programming Fundamentals Fall 17 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Array. Prepared By - Rifat Shahriyar

Full file at

Fundamentals of Programming Data Types & Methods

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 2: Using Data

Formatting Output & Enumerated Types & Wrapper Classes

Lesson 3: Accepting User Input and Using Different Methods for Output

PROGRAMMING FUNDAMENTALS

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Java Identifiers, Data Types & Variables

Topics. The Development Process

Chapter 2: Java Fundamentals

8/23/2014. Chapter Topics. Chapter Topics (2) Parts of a Java Program. Parts of a Java Program. Analyzing The Example. Chapter 2: Java Fundamentals

Class Libraries and Packages

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Introduction to Classes and Objects

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Chapter 02: Using Data

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Computational Expression

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

19. GUI Basics. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

Introduction to Computers and Engineering Problem Solving 1.00 / Fall 2004

Example packages from Java s standard class library:

Chapter 2: Using Data

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Introduction to Java

COMP6700/2140 Std. Lib., I/O

In this lab we will practice creating, throwing and handling exceptions.

H212 Introduction to Software Systems Honors

Chapter 3 - Introduction to Java Applets

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

IQTIDAR ALI Lecturer IBMS Agriculture University Peshawar

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

JAVA WRAPPER CLASSES

Course Outline. Introduction to java

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

CSC Java Programming, Fall Java Data Types and Control Constructs

Full file at

Array. Array Declaration:

CS 11 java track: lecture 1

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

Input. Scanner keyboard = new Scanner(System.in); String name;

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

Text User Interfaces. Keyboard IO plus

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Java Basic Programming Constructs

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Language Features. 1. The primitive types int, double, and boolean are part of the AP

VARIABLES AND TYPES CITS1001

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Chapter 2 ELEMENTARY PROGRAMMING

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

TTh 9.25 AM AM Strain 322

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Chapter 4 Defining Classes I

Packages & Random and Math Classes

Java Reference Card. 1. Classes. 2. Methods. 3. Conditionals. 4. Operators

Programming with Java

Following is the general form of a typical decision making structure found in most of the programming languages:

CS162: Introduction to Computer Science II

Question: Total Points: Score:

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Question: Total Points: Score:

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

All classes in a package can be imported by using only one import statement. If the postcondition of a method is not met, blame its implementer

The static Keyword. Lecture 2 Java Intermediate. The static Keyword. The Plan. Class Attributes The static Keyword. Class Methods The static Keyword

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

Full file at

Motivation was to facilitate development of systems software, especially OS development.

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

CT 229 Java Syntax Continued

JAVA GUI PROGRAMMING REVISION TOUR III

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Recitation: Loop Jul 7, 2008

Transcription:

Data Types Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To differentiate between Java defined types and user defined types To have a deeper understanding of the String class To get acquainted with the Math class To get acquainted with the Wrapper classes

Data Types Reference Types Reference types - any data type that is composed of primitive data types as its based type. In other words, it is an aggregate of primitive types. Reference types in Java are: The array data structure, and The class data type. In this section we will briefly discuss the concept of array. We will discuss the class type to the extent of the fundamental classes of Java.

Reference Types - Array Array An array is a set of storage locations set aside to hold one type of data. It is simply a means where by we can store values of the same type by using one generic name. The list of items are stored linearly Hence the items can be accessed by their relative position in the list. Arrays are real objects in Java. Storage space is allocated for an array during execution time. The concept will be discussed fully in future lesson

Reference Types - Array Data arr 10 20 30 40 50 60 70 80 0 1 2 3 4 5 6 7 indeces This is the most we will say about arrays for now

Reference Types - Class The concept of class as you know is the fundamental construct upon which Java is built. A class serves as a blueprint or a template for a certain type of object. All data types other than the primitive type or the array must be addressed in terms of class. This is evident in the way that we have been using class. For instance, going back to the class Book. In order to use this class we had to declare variables of the type: That is, Book b1; for instance

Reference Types - Standard Packages Java has a vast and rich collection of classes. These classes are stored in respective directories and subdirectories. Each directory or subdirectory is called, a package. Programmers can use these classes in their specific applications. There are several packages in Java. Two of the most important packages in the language are java and javax.

Java Standard Packages java and javax packages, along with their sub-packages. Some of the subpackages also contain sub-packages of their own The sub-package lang is a special package is for general programming The sub-package javax is designed for Graphical User Interface programming (GUI)

Java Standard Package- lang The java.lang package contains classes that are fundamental to Java. All of the classes in this package are available in your program automatically. That is, as soon as the Java compiler is loaded, all of these classes are loaded into every Java source file.

Java Standard Package- lang

Java Standard Package- lang Chief among these fundamental classes are the following: Object String Math System The wrapper classes - for each primitive type there is a corresponding class. Byte Short Integer Long Character Float Double Boolean Void

The class Object See the Application Programmers Interface (API) Class String Math Wrapper classes - Byte, Short, Integer, Long, Character, Float, Double, Boolean, and Void.

Input and Output Operations There are three other important operations that are performed on data. Input operations Output operations, and Formatting operations. We have been generating outputs already but it was not dealt with formally. Input Operations - There are several ways to input data into a program: Java Input/Output classes The DOS prompt The Scanner Class JOptionPane Class

JOptionPane Input/Output Dialog Boxes Capabilities 1. Create input dialog boxes 2. Create output windows

JOptionPane Input Dialog Boxes There are four kinds of standard dialogs: 1. Message dialog shows a message and waits for the user to click OK. 2. Confirmation dialog shows a question and ask for confirmation such as OK or Cancel. 3. Option dialog shows a question and gets the user s response from a set of options. 4. Input dialog shows a question and gets the user s input from a text field, a combo box, or list.

Enhance Your Output with Scrollable Window

JOptionPane Input Dialog You must import the class JOptionPane import javax.swing.joptionpane; The class contains: Several constructors, and Several class methods that deals with dialog boxes. The general construct of these methods is as follows: showxxxxdialog( parameter_list) Where XXXX is any of the four dialog box types. We will only be concerned with the input dialog type.

Using JOptionPane for Input 1. import javax.swing.joptionpane; 2. class optionpane 3. { 4. public static void main(string[] arg) 5. { 6. String str = JOptionPane.showInputDialog( "Read data ); 7. } 8. }

Using JOptionPane for Input Type the value 123.45 This value is returned as a String String str = JOptionPane.showInputDialog( "Read data ); That is, str = 123.45

Using the Wrapper class - Double Convert the string value to a double by using the parsedouble method 1. import javax.swing.joptionpane; 2. class convert_string 3. { 4. public static void main(string[] arg) 5. { 6. String str = JOptionPane.showInputDialog("Read data"); 7. double x = Double.parseDouble(str); 8. } 9. }

Build a Single Class for Input 1. import javax.swing.joptionpane; 2. class GetData 3. { 4. static String str; 5. static double getdouble(string s) 6. { 7. str = JOptionPane.showInputDialog(s); 8. return Double.parseDouble(str); 9. } 10. static int getint(string s) 11. { 12. str = JOptionPane.showInputDialog(s); 13. return Integer.parseInt(str); 14. } 15. static String getword(string s) 16. { 17. return JOptionPane.showInputDialog(s); 18. } 19. }

Build a Single Class for Input (Another view) 1. import javax.swing.joptionpane; 2. class GetData 3. { 4. static double getdouble(string s) 5. { 6. return Double.parseDouble( getword(s) ); 7. } 8. static char getchar(string s) 9. { 10. String str = getword(s); 11. return str.charat(0); 12. } 13. static int getint(string s) 14. { 15. return Integer.parseInt(getWord(s)); 16. } 17. static String getword(string s) 18. { 19. return JOptionPane.showInputDialog(s); 20. } 21. }

Using the GetData Class 1. class TestGetData 2. { 3. public static void main(string[] arg) 4. { 5. int x = GetData.getInt("Type an integer value"); 6. double y = GetData.getDouble("Type a double value"); 7. String name = GetData.getWord("Enter a Name"); 8. System.out.println("Your name is: " + name + "\nyour age is: " 9. + x + "\nyou have $" + y); 10. } 11. }

yy Dialog box when Line 5 is executed. Dialog box when Line 6 is executed. Dialog box when Line 7 is executed.

The output The output