Java Classes. GEEN163 Introduction to Computer Programming



Similar documents
Moving from CS 61A Scheme to CS 61B Java

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

More on Objects and Classes

CS 111 Classes I 1. Software Organization View to this point:

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Chapter 1 Java Program Design and Development

Introduction to Object-Oriented Programming

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Chapter 2 Introduction to Java programming

Object-Oriented Programming in Java

Java CPD (I) Frans Coenen Department of Computer Science

CS 106 Introduction to Computer Science I

LAB4 Making Classes and Objects

Part I. Multiple Choice Questions (2 points each):

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Many applications consist of one or more classes, each containing one or more methods. If you become part of a development team in industry, you may

Computer Programming I & II*

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Syllabus for CS 134 Java Programming

Object-Oriented Programming Lecture 2: Classes and Objects

Chapter 2: Elements of Java

Basic Object-Oriented Programming in Java

#820 Computer Programming 1A

Install Java Development Kit (JDK) 1.8

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Curriculum Map. Discipline: Computer Science Course: C++

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Taxi Service Coding Policy. Version 1.2

Computer Programming I

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Java Program Coding Standards Programming for Information Technology

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

J a v a Quiz (Unit 3, Test 0 Practice)

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Java Programming Language

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Introduction to Programming

CSE 308. Coding Conventions. Reference

Programming Languages CIS 443

Computer Programming I

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Java Interview Questions and Answers

The C Programming Language course syllabus associate level

Glossary of Object Oriented Terms

Course Title: Software Development

Introduction to Java. CS 3: Computer Programming in Java

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

ECE 341 Coding Standard

Introduction to Java

Java Basics: Data Types, Variables, and Loops

Monitors, Java, Threads and Processes

Java from a C perspective. Plan

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

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Description of Class Mutation Mutation Operators for Java

Crash Course in Java

AVRO - SERIALIZATION

Java Crash Course Part I

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Problem Solving. Software Engineering. Behavior. OCD in a Nutshell. Behavior. Objects Operations Algorithm. Object-Centered Design

Java is an Object-Oriented Language. As a language that has the Object Oriented feature, Java supports the following fundamental concepts:

Object Oriented Software Design II

High-Level Language. Building a Modern Computer From First Principles.

Java (12 Weeks) Introduction to Java Programming Language

Realizing Enterprise Integration Patterns in WebSphere

Chapter 6: Programming Languages

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Fundamentals of Java Programming

FLORIDA STATE COLLEGE AT JACKSONVILLE COLLEGE CREDIT COURSE OUTLINE. Introduction to Programming with Visual Basic.NET

Note: Syntactically, a ; is needed at the end of a struct definition.

Contents. 9-1 Copyright (c) N. Afshartous

Web development... the server side (of the force)

AP Computer Science Java Subset

java Features Version April 19, 2013 by Thorsten Kracht

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Chapter 1 Fundamentals of Java Programming

Lecture 5: Java Fundamentals III

WRITING DATA TO A BINARY FILE

Variables, Constants, and Data Types

An Overview of Java. overview-1

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Inheritance, overloading and overriding

JAVA - OBJECT & CLASSES

An Introduction to Object-Oriented Programming with

Using Files as Input/Output in Java 5.0 Applications

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Objects and classes. Objects and classes. Jarkko Toivonen (CS Department) Programming in Python 1

Transcription:

Java Classes GEEN163 Introduction to Computer Programming

Never interrupt someone doing what you said couldn't be done. Amelia Earhart

Classes, Objects, & Methods Object-oriented programming uses classes, objects, and methods as basic programming components. These components help to organize a large program into small modules design and think about an intricate program abstract and isolate concepts

Nomenclature Class defines a kind of object Object an instance of a class. Instantiate creating an object of a class Instance An object is an instance of a class Method coordinated sequence of instructions or function that an object can perform.

Classes and Objects Programmers can define new classes in a Java program You can create objects from existing classes in your programs

Defining a Java Class An object should contain the information about something. An object should encapsulate a concept. A class is a new data type, like double or String. You can create new data types as you need in Java.

Syntax of a Java Class Definition class ClassName { // data declarations // methods

Example Class public class Widget { private int count = 0; // class data value public void inc() { // method to increment count++; public int getcount() { // accessor method return count; public void setcount( int value ) { // modifier method count = value;

The Widget class does not have a main method because A. it is an applet B. it is to be used in another program C. it has private data D. the inc method serves as the main

Declaring an Object Imagine we have a class Widget We can declare an object of type Widget just like we declare a variable to be an int or a double. Widget thing; Widget dodad, whatchamacallit;

Instantiating Objects Widget something = new Widget(); Widget zebra; zebra = new Widget(); After the word new is a call to a constructor method that helps create the object. The constructor method may or may not have parameters.

Reference Variables When you declare a primitive data item, such as a double or int, Java reserves some memory to store the data When you declare an object, Java does not reserve space for the object until it is created You can instantiate a new object with the keyword new

Object References When you declare an object, you are creating a reference to the object Widget rat; rat Widget weasel; null weasel null

Object References When you declare an object, you are creating a reference to the object Widget rat; rat Widget weasel; Widget rat = new Widget(); weasel null

Object References When you declare an object, you are creating a reference to the object Widget rat; rat Widget weasel; Widget rat = new Widget(); weasel = rat; weasel

Objects have Data An object can have data variables. Data values are called Fields or Properties Fields can be primitive data types, such as double or int, or they can be other objects

What is displayed? Widget cat = new Widget(); Widget dog = new Widget(); cat.setcount(5); dog.setcount(7); cat = dog; dog.setcount(3); System.out.println( cat.getcount() ); A. 3 B. 5 C. 7 D. none of the above

Field Declaration Field variables are declared within the class definition, but outside of any method. public class Student { int public double public String identifier; grade; name;

Accessing Object Fields The field variables of an object can be used by any method of that object just like a variable defined in the method. public class Student { int identifier; public double grade; public String name; public void setgrade(double score) { grade = score;

Access Outside the Class Public field variables can be accessed from outside of the class. To access a field variable, write the object name, a period and then the field variable s name Student fred = new Student(); fred.name = Fred Smith ;

public and private Public data items can be accessed anywhere. Public methods can be called by any part of the program. Private data items can be accessed only by methods of the class. Private methods can only be called by other methods of the class.

Private example public class Student { public int id; private double grade; public String name;

Accessing the Example In another class, you can access the public but not the private values public class myprog { public static void main(string[] x ) { Student fred = new Student(); fred.name = Sally ; fred.grade = 4.0; // not allowed

Data Abstraction Classes are described by their interface, that is, what they can do and how they are used. The internal data and operation are hidden. In this way if the internal operation is changed, programs using the class will not be impacted as long as the interface remains consistent.

Variables A method can use three different types of variables local variables defined in the method parameter variables object field variables Object field variables can be used in any of the methods of the class

Constructors Initialize an object Modifiers Method Purpose Change the values of an object Accessors Return the value of a object without changing anything Function Compute some value or perform some action

Constructors A constructor method is automatically called when an object is created. The name of the constructor method is always the same as the class name. Constructors can be used to initialize the values of an object s variables. Constructors may or may not have parameters.

Constructor Example public class Widget { private int count = 0; /* constructor method */ public Widget( int initial ) { count = initial;

Another Constructor Example public class Widget { private int count = 0; /* constructor method */ public Widget( int count ) { this.count = count;

Tryit public class Rodent{ double rat; int mouse; // Write a constructor method to initialize // the class variables // in another program using the Rodent class Rodent mole = new Rodent( 5.6, 14 ); Rodent gerbil = new Rodent( 3.25, 8 );

Possible Constructor public class Rodent{ double rat; int mouse; public Rodent( double vole, int shrew ) { rat = vole; mouse = shrew; // in a program using the Rodent class Rodent mole = new Rodent( 5.6, 14 ); Rodent gerbil = new Rodent( 3.25, 8 );

this The Java keyword this means this object You can use this to access anything of the object, but not the class If you have a field named xyz, then you can access the field as this.xyz

Accessor Methods Accessor methods return some value from the object. Accessor methods do not change anything in the object. Examples of accessor methods include: String length() method.

Modifier Methods Modifier methods change the state of an object A modifier method may just set the value of a single variable or may perform a lengthy sequence of actions Modifier methods are often void methods Examples of modifier methods include: setcount from the earlier example

Naming Convention Class names start with an uppercase letter Objects are written in lowercase Method names are in lowercase Constructors must have the same name as the class Accessor methods have names that can be used as nouns Modifier methods have names that can be used as verbs

Calling Methods In Java you can use a method of an object by writing the object s name, a period, the name of the method. Widget thing = new Widget(); thing.whatever( 5 ); This calls the whatever method of Widget object thing passing it an integer 5 as a parameter

Inheritance You can extend an existing class to add new features The new class has all of the data values and methods of the parent classes

Inheritance Example Some of our programs have input numbers from the JTextField The gettext method of JTextField only returns a string We can extend JTextField to add methods to get a double or an int

Extending JTextField public class NumField extends javax.swing.jtextfield { public int getint() { String text = gettext(); int number = Integer.parseInt(text); return number; public double getdouble() { String text = gettext(); double number = Double.parseDouble(text); return number;

Using NumField NumField inyear = new NumField(); NumField inloan = new NumField(); public void actionperformed( java.awt.event.actionevent thing) { double loan = inloan.getdouble(); double m = inyear.getdouble() * 12.0; double pay = loan * etc.; answer.settext("monthly payments of $"+pay);

No Program This Week There is no programming homework this week