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

Size: px
Start display at page:

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

Transcription

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

2 Lesson Goals Understand Object Oriented Programming The Syntax of Class Definitions Constructors this Object Oriented "Hello World" example Much of the information in this section is adapted from: 2

3 What Is Object Oriented Programming Object-Oriented programming consists of three stages: Creating the Program The programmer creates source files that define classes for objects the program will use when it is running. The programmer defines a class that contains the static main() method which is used to start the program running. Compiling the Program The program is compiled into bytecode. There may be several source files. Each is compiled into bytecode. Running the Program The java interpreter looks for a static main() method and starts running it. Since main() is static it can start running even though no objects have been created, yet. As the program runs, objects are created and their methods are activated. The program does its work by creating objects and activating their methods. The exact order of object creation and method activation depends on the task to be performed and the input data. 3

4 Communicating Objects (1) A running program is a collection of objects that are each doing their own task and communicating with other objects. The picture (next slide) shows a running program for a pizza shop. The jagged lines represent communication. The boss is special and so is drawn with dotted lines. In Java programs the main() method is special because it is where the Java interpreter starts the whole program running. The main() method is a static method, which means that there will be only one instance of it and that it exists (as part of a class) before any objects have been created. 4

5 Communicating Objects (2) 5

6 The Syntax Of A Class Definition Modifiers class ClassName // Description of the instance variables // Description of the constructors // Description of the methods A few notes: For now, replace modifiers with public in the class that contains main and don't include it in other classes in the same file. Separating the class into sections is done for clarity. It is not a rule of the language. A simple class might have just a few variables and be defined in just a few lines of code. A large, complicated class might take thousands of lines of code for its definition. 6

7 What Is UML UML, short for Unified Modeling Language, is a standardized modeling language consisting of an integrated set of diagrams, developed to help system and software developers for specifying, visualizing, constructing, and documenting the artifacts of software systems, as well as for business modeling and other non-software systems. The UML represents a collection of best engineering practices that have proven successful in the modeling of large and complex systems. The UML is a very important part of developing object oriented software and the software development process. The UML uses mostly graphical notations to express the design of software projects. Using the UML helps project teams communicate, explore potential designs, and validate the architectural design of the software. If you become a software engineer you will be using UML throughout your career. 7

8 UML Class Description There are several slightly different representations of classes in UML, but they all express the same information Object Name Instance Variables Constructors and Methods 8

9 Access Modifiers (1) 9

10 Access Modifiers (2) Access Modifier Determines access rights for the class and its members Defines where the class and its members can be used Classes are usually declared to be public. Instance variables are usually declared to be private. Methods that will be called by the client of the class are usually declared to be public. APIs of methods are published (made known) so that clients will know how to instantiate objects and call the methods of the class. Methods that will be called only by other methods of the class are usually declared to be private. 10

11 An Extremely Simple First Example (1) We will be looking at HelloTester.java, HelloTester2.java and HelloObject2.java files HelloTester.java is a complete program which includes two class definitions The definition of class HelloObject includes a method but no instance variables, so objects of class HelloObject have no instance variables. The class does have a constructor but it is not explicitly defined in the There is a default constructor that is used to create an object when no constructor is specified Class HelloTester contains only the static main() method When the main() method starts, it constructs a HelloObject and then invokes that object's speak() method. The variable anobject is a reference variable that points to the newly created object. Note: If you are compiling and running Java from the command line, both classes can be put into one file. The name of the file must match the name of the class that contains the main() method, so this file must be named HelloObject.java. The class that contains main() must use the public modifier. Don't use any modifiers for the other classes. Upper and lower case must match in the file name and in the class name. 11

12 An Extremely Simple First Example (2) I will put each class in a separate file, and this is what is required in any IDE Keep all the files for any program in the same directory You can then compile the file with the class containing the main method, and all other files will be automatically compiled see HelloTester2.java and HelloObject2.java Java specifies that only one public class can be saved in any file Therefore, if you want to use a class in multiple different programs the class must be saved in a separate file should use the public modifier. The name of the file must be classname.java. 12

13 The class HelloObject The simple class below has no instance variables and uses the default constructor class HelloObject // 2a. The class definition is // used to make the object public void speak() // 2b. A speak() method is included in the object. // It must be public to allow accessibility from other classes System.out.printf((( %s\n, "Hello from an object!"); // 3a. The speak() method // of the object prints a // message on the screen. // 3b. The method returns to the // caller. public class HelloTester public static void main ( String[] args ) // 1. Main starts running. HelloObject anobject = new HelloObject(); // 2. A HelloObject // is created using its anobject.speak(); // default constructor. // 3. The object's speak() // method is called. // 4. The entire program is finished. This is the same as calling a method for an imported class - keyboard.nextint() Remember object.method 13

14 The Driver Class The Java interpreter starts a program by looking for a static main() method inside of the HelloTester.class file. Since the method is static the interpreter can run it without first constructing an object. It is convenient to have a separate class that serves no other purpose than to contain the main() method. This testing class is used to start things running. Sometimes the class that contains main() is called a driver Usually main() constructs objects of various classes and calls their methods. These objects do the real work of the program. The source file for the program is named HelloTester.java. When you compile the file, the compiler outputs two separate files of bytecodes, one for each class (see directory for class files) 14

15 The Default Constructor There is no constructor in HelloObject so it uses the default object The default constructor does a great deal of work behind the scenes. It works with the Java virtual machine to find main memory for the object, sets up that memory as an object, puts in the variables and methods specified in the class definition, and returns an object reference to your program. If you need to initialize some variables in a newly constructed object, then write a constructor as part of the class. All your constructor needs is statements that initialize the variables. The "behind the scenes" work is still included automatically. If you define one or more constructors for a class, then those are the only constructors that the class has. The default constructor is supplied automatically only if you define no constructors. 15

16 Constructors (1) Most objects are instantiated with parameters for some instance variables Scanner keyboard = new Scanner(System.in); has System.in as the parameter that may be used by the constructor The format of a constructor looks very much like the format for a method: public classname( parameterlist ) Statements involving the instance variables of the class and the parameters in the parameterlist. What s different? No return values and therefore no return statements Fewer modifiers use public for now all others include private, protected or no modifier at all 16

17 Constructors (2) A constructor returns a reference to the object it constructs. Don't put a return type in front of classname and don't use a return statement in the body of the constructor. The constructor has the same name as the class. The parameterlist is a list of values and their types: TypeName1 parametername1, TypeName2 parametername2,... as many as you need It is OK to have an empty parameter list. A class often has several constructors with different parameters. Each one builds the same class of object, but the different constructors use different sources of data for object initialization. This means that a constructor can be overloaded Usually the method that invokes a constructor saves the returned reference in a variable. Sometimes an object is constructed for temporary use and its reference is not saved. The object is used once for some brief purpose and then becomes garbage. 17

18 Defining Instance Variables Syntax: accessmodifier datatype identifierlist; datatype can be a primitive data type or a class type. identifierlist can contain: One or more variable names of the same data type Multiple variable names separated by commas Initial values Optionally, instance variables can be declared as final. So in fact instance variables are variables of the class public instance variables can be accessed directly object.variablename - equivalent to arrayname.length private instance variables require an accessor method object.method() - equivalent to stringvar.length() Note: Instance variables of a class are global to the class In Java instance variables are initialized the same way they are in arrays appropriate 0 for integer or floating point, false for boolean, and the null char for a character. The address for any object variable that is an instance variable for a class is initialized to null. 18

19 Example With A Constructor class HelloWithConstructor private String greeting; // An instance variable accessible only to methods within the class (it is private) public HelloWithConstructor( String st ) // The constructor greeting = st; public void speak() //A method of the class System.out.printf( %s\n, greeting ); public class HelloTesterWithConstructor public static void main ( String[] args ) HelloWithConstructor anobject = new HelloWithConstructor("A Greeting!"); anobject.speak(); 19

20 Another Example Creating Multiple Objects public class HelloTesterWithConstructorV2 public static void main ( String[] args ) HelloWithConstructor object1 = new HelloWithConstructor("Mercury"); HelloWithConstructor object2 = new HelloWithConstructor("Venus"); HelloWithConstructor object3 = new HelloWithConstructor("Earth"); HelloWithConstructor object4 = new HelloWithConstructor("Mars"); object1.speak(); object2.speak(); object3.speak(); object4.speak(); 20

21 this The keyword this is used to represent an instance of the class in which it appears. this can be used to access class members and as a reference to the current instance The this keyword is also used to forward a call from one constructor in a class to another constructor in the same class. We usually use this when referring to an instance variable in order to avoid confusion. class HelloWithConstructorV3 private String greeting; //instance variable, accessible throughout the class public HelloWithConstructorV3( String greeting ) this.greeting = greeting; // use to distinguish from parameter greeting variable within the constructor public void speak() System.out.printf(( %s\n, greeting ); 21

22 A class Can Have Multiple Constructors The same overloading rules apply to constructors as apply to methods Same number of parameters but at least one of a different type Different number of parameters public class HelloWithSeveralConstructors private String greeting; //instance variable, accessible throughout the class public HelloWithSeveralConstructors() // constructor with no parameters this.greeting = "Hello"; public HelloWithSeveralConstructors( String greeting ) // constructor with one parameter this.greeting = greeting; public HelloWithSeveralConstructors( char greeting) this.greeting = greeting + ""; public void speak() System.out.printf("%s\n",greeting ); 22

23 Programming Exercise 1 TestRectangle Write a class Rectangle that is instantiated with a length and width, each as type double. Include methods that will calculate the perimeter and area, and return those values. Create a default constructor without parameters that sets the length and width to 1.0 Then write a class TesterRectangle that reads in a length and width, creates a Rectangle, call the appropriate methods and prints out the length and width. Do the same thing creating a Rectangle with the default constructor. 23

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166 Lecture 13 & 14 Single Dimensional Arrays Dr. Martin O Connor CA166 www.computing.dcu.ie/~moconnor Table of Contents Declaring and Instantiating Arrays Accessing Array Elements Writing Methods that Process

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Define and discuss abstract classes Define and discuss abstract methods Introduce polymorphism Much of the information

More information

Lecture 7: Classes and Objects CS2301

Lecture 7: Classes and Objects CS2301 Lecture 7: Classes and Objects NADA ALZAHRANI CS2301 1 What is OOP? Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Array. Prepared By - Rifat Shahriyar

Array. Prepared By - Rifat Shahriyar Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types

More information

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.

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 How to Program, 9/e Education, Inc. All Rights Reserved. } Covered in this chapter Classes Objects Methods Parameters double primitive type } Create a new class (GradeBook) } Use it to create an object.

More information

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

Selected Questions from by Nageshwara Rao

Selected Questions from  by Nageshwara Rao Selected Questions from http://way2java.com by Nageshwara Rao Swaminathan J Amrita University swaminathanj@am.amrita.edu November 24, 2016 Swaminathan J (Amrita University) way2java.com (Nageshwara Rao)

More information

A Formal Presentation On Writing Classes With Rules and Examples CSC 123 Fall 2018 Howard Rosenthal

A Formal Presentation On Writing Classes With Rules and Examples CSC 123 Fall 2018 Howard Rosenthal A Formal Presentation On Writing Classes With Rules and Examples CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Defining a Class Defining Instance Variables Writing Methods The Object Reference this The

More information

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal The ArrayList class CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Describe the ArrayList class Discuss important methods of this class Describe how it can be used in modeling Much of the information

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS Computers process information and usually they need to process masses of information. In previous chapters we have studied programs that contain a few variables

More information

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes Lecture Contents 2 What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class In Java 3 Java is an object-oriented

More information

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

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved. 1 3 Introduction to Classes and Objects 2 You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility.

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Lab5. Wooseok Kim

Lab5. Wooseok Kim Lab5 Wooseok Kim wkim3@albany.edu www.cs.albany.edu/~wooseok/201 Question Answer Points 1 A or B 8 2 A 8 3 D 8 4 20 5 for class 10 for main 5 points for output 5 D or E 8 6 B 8 7 1 15 8 D 8 9 C 8 10 B

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

Repetition CSC 121 Fall 2014 Howard Rosenthal

Repetition CSC 121 Fall 2014 Howard Rosenthal Repetition CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

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

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved. 1 3 Introduction to Classes and Objects 2 You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility.

More information

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com CHAPTER 8 OBJECTS AND CLASSES Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/2011 Chapter Goals To understand the concepts of classes, objects and encapsulation To implement instance variables,

More information

2.8. Decision Making: Equality and Relational Operators

2.8. Decision Making: Equality and Relational Operators Page 1 of 6 [Page 56] 2.8. Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. This section introduces a simple version of Java's if statement

More information

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods

More information

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Repe$$on CSC 121 Spring 2017 Howard Rosenthal Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when

More information

Topics. Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2

Topics. Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2 Classes & Objects Topics Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2 Object-Oriented Programming Classes combine data and the methods (code) to manipulate the

More information

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repe$$on CSC 121 Fall 2015 Howard Rosenthal Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Brief Summary of Java

Brief Summary of Java Brief Summary of Java Java programs are compiled into an intermediate format, known as bytecode, and then run through an interpreter that executes in a Java Virtual Machine (JVM). The basic syntax of Java

More information

Introduction to Classes and Objects

Introduction to Classes and Objects 1 2 Introduction to Classes and Objects You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility.

More information

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

Chapter 6 Lab Classes and Objects

Chapter 6 Lab Classes and Objects Gaddis_516907_Java 4/10/07 2:10 PM Page 51 Chapter 6 Lab Classes and Objects Objectives Be able to declare a new class Be able to write a constructor Be able to write instance methods that return a value

More information

Fundamental Concepts and Definitions

Fundamental Concepts and Definitions Fundamental Concepts and Definitions Identifier / Symbol / Name These terms are synonymous: they refer to the name given to a programming component. Classes, variables, functions, and methods are the most

More information

Express Yourself. Writing Your Own Classes

Express Yourself. Writing Your Own Classes Java Programming 1 Lecture 5 Defining Classes Creating your Own Classes Express Yourself Use OpenOffice Writer to create a new document Save the file as LastFirst_ic05 Replace LastFirst with your actual

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

System.out.print(); Scanner.nextLine(); String.compareTo();

System.out.print(); Scanner.nextLine(); String.compareTo(); System.out.print(); Scanner.nextLine(); String.compareTo(); Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 6 A First Look at Classes Chapter Topics 6.1 Objects and

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

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

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba Laboratory Session: Exercises on classes Analogy to help you understand classes and their contents. Suppose you want to drive a car and make it go faster by pressing down

More information

Binghamton University. CS-140 Fall Data Types in Java

Binghamton University. CS-140 Fall Data Types in Java Data Types in Java 1 CS-211 2015 Example Class: Car How Cars are Described Make Model Year Color Owner Location Mileage Actions that can be applied to cars Create a new car Transfer ownership Move to a

More information

Fundamentals of Programming Data Types & Methods

Fundamentals of Programming Data Types & Methods Fundamentals of Programming Data Types & Methods By Budditha Hettige Overview Summary (Previous Lesson) Java Data types Default values Variables Input data from keyboard Display results Methods Operators

More information

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017 Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 07: OOP Part 1 Instructor: AbuKhleif, Mohammad Noor Sep 2017 Instructor AbuKhleif, Mohammad Noor Computer Engineer (JU

More information

Programming overview

Programming overview Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in

More information

Chapter 4. Defining Classes I

Chapter 4. Defining Classes I Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object oriented programming (OOP) possible Programming in Java consists of dfii defining a number of

More information

4. Java language basics: Function. Minhaeng Lee

4. Java language basics: Function. Minhaeng Lee 4. Java language basics: Function Minhaeng Lee Review : loop Program print from 20 to 10 (reverse order) While/for Program print from 1, 3, 5, 7.. 21 (two interval) Make a condition that make true only

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

Lecture Notes for CS 150 Fall 2009; Version 0.5

Lecture Notes for CS 150 Fall 2009; Version 0.5 for CS 150 Fall 2009; Version 0.5 Draft! Do not distribute without prior permission. Copyright 2001-2009 by Mark Holliday Comments, corrections, and other feedback appreciated holliday@email.wcu.edu Chapter

More information

Chapter 6 Lab Classes and Objects

Chapter 6 Lab Classes and Objects Lab Objectives Chapter 6 Lab Classes and Objects Be able to declare a new class Be able to write a constructor Be able to write instance methods that return a value Be able to write instance methods that

More information

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

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations CS162: Introduction to Computer Science II Primitive Types Java Fundamentals 1 2 Primitive types The eight primitive types in Java Primitive types: byte, short, int, long, float, double, char, boolean

More information

Chapter 10 Introduction to Classes

Chapter 10 Introduction to Classes C++ for Engineers and Scientists Third Edition Chapter 10 Introduction to Classes CSc 10200! Introduction to Computing Lecture 20-21 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this

More information

CS 101 Fall 2006 Midterm 1 Name: ID:

CS 101 Fall 2006 Midterm 1 Name:  ID: You only need to write your name and e-mail ID on the first page. This exam is CLOSED text book, closed-notes, closed-calculator, closed-neighbor, etc. Questions are worth different amounts, so be sure

More information

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Review what an array is Review how to declare arrays Review what reference variables are Review how to pass arrays to methods Review

More information

Methods CSC 121 Fall 2016 Howard Rosenthal

Methods CSC 121 Fall 2016 Howard Rosenthal Methods CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods

More information

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Lesson 06 Arrays MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Array An array is a group of variables (called elements or components) containing

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

CS 251 Intermediate Programming Java Basics

CS 251 Intermediate Programming Java Basics CS 251 Intermediate Programming Java Basics Brooke Chenoweth University of New Mexico Spring 2018 Prerequisites These are the topics that I assume that you have already seen: Variables Boolean expressions

More information

9 Working with the Java Class Library

9 Working with the Java Class Library 9 Working with the Java Class Library 1 Objectives At the end of the lesson, the student should be able to: Explain object-oriented programming and some of its concepts Differentiate between classes and

More information

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

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

CSC Java Programming, Fall Java Data Types and Control Constructs

CSC Java Programming, Fall Java Data Types and Control Constructs CSC 243 - Java Programming, Fall 2016 Java Data Types and Control Constructs Java Types In general, a type is collection of possible values Main categories of Java types: Primitive/built-in Object/Reference

More information

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming s SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177 Programming Time allowed: THREE hours: Answer: ALL questions Items permitted: Items supplied: There is

More information

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness. Methods There s a method in my madness. Sect. 3.3, 8.2 1 Example Class: Car How Cars are Described Make Model Year Color Owner Location Mileage Actions that can be applied to cars Create a new car Transfer

More information

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods

More information

Chapter 1 Getting Started

Chapter 1 Getting Started Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different

More information

Assumptions. History

Assumptions. History Assumptions A Brief Introduction to Java for C++ Programmers: Part 1 ENGI 5895: Software Design Faculty of Engineering & Applied Science Memorial University of Newfoundland You already know C++ You understand

More information

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods

More information

Object Oriented Modeling

Object Oriented Modeling Object Oriented Modeling Object oriented modeling is a method that models the characteristics of real or abstract objects from application domain using classes and objects. Objects Software objects are

More information

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

Lesson 3: Accepting User Input and Using Different Methods for Output Lesson 3: Accepting User Input and Using Different Methods for Output Introduction So far, you have had an overview of the basics in Java. This document will discuss how to put some power in your program

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

CS162: Introduction to Computer Science II

CS162: Introduction to Computer Science II CS162: Introduction to Computer Science II Java Fundamentals 1 Primitive Types 2 1 Primitive types: Primitive types byte, short, int, long, float, double, char, boolean Example: int size = 42; size is

More information

Chapter 3 Objects and Classes

Chapter 3 Objects and Classes Chapter 3 Objects and Classes Topics OO Programming Concepts Creating Objects and Object Reference Variables Constructors Modifiers Instance and Class Variables and Methods Scope of Variables OO Programming

More information

CS260 Intro to Java & Android 03.Java Language Basics

CS260 Intro to Java & Android 03.Java Language Basics 03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance

More information

Software Development With Java CSCI

Software Development With Java CSCI Software Development With Java CSCI-3134-01 D R. R A J S I N G H Outline Week 8 Controlling Access to Members this Reference Default and No-Argument Constructors Set and Get Methods Composition, Enumerations

More information

Interfaces CSC 123 Fall 2018 Howard Rosenthal

Interfaces CSC 123 Fall 2018 Howard Rosenthal Interfaces CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Defining an Interface How a class implements an interface Using an interface as a data type Default interfaces Implementing multiple interfaces

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE

More information

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. Data structures Collections of related data items. Discussed in depth in Chapters 16 21. Array objects Data

More information

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Inheritance CSC 123 Fall 2018 Howard Rosenthal Inheritance CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Defining what inheritance is and how it works Single Inheritance Is-a Relationship Class Hierarchies Syntax of Java Inheritance The super Reference

More information

ECOM 2324 COMPUTER PROGRAMMING II

ECOM 2324 COMPUTER PROGRAMMING II ECOM 2324 COMPUTER PROGRAMMING II Object Oriented Programming with JAVA Instructor: Ruba A. Salamh Islamic University of Gaza 2 CHAPTER 9 OBJECTS AND CLASSES Motivations 3 After learning the preceding

More information

BM214E Object Oriented Programming Lecture 8

BM214E Object Oriented Programming Lecture 8 BM214E Object Oriented Programming Lecture 8 Instance vs. Class Declarations Instance vs. Class Declarations Don t be fooled. Just because a variable might be declared as a field within a class that does

More information

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs Chapter 8 User-Defined Classes and ADTs Objectives To understand objects and classes and use classes to model objects To learn how to declare a class and how to create an object of a class To understand

More information

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while CSCI 150 Fall 2007 Java Syntax The following notes are meant to be a quick cheat sheet for Java. It is not meant to be a means on its own to learn Java or this course. For that you should look at your

More information

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation. Java: Classes Introduction A class defines the abstract characteristics of a thing (object), including its attributes and what it can do. Every Java program is composed of at least one class. From a programming

More information

Introduction to Java https://tinyurl.com/y7bvpa9z

Introduction to Java https://tinyurl.com/y7bvpa9z Introduction to Java https://tinyurl.com/y7bvpa9z Eric Newhall - Laurence Meyers Team 2849 Alumni Java Object-Oriented Compiled Garbage-Collected WORA - Write Once, Run Anywhere IDE Integrated Development

More information

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

BASIC INPUT/OUTPUT. Fundamentals of Computer Science BASIC INPUT/OUTPUT Fundamentals of Computer Science Outline: Basic Input/Output Screen Output Keyboard Input Simple Screen Output System.out.println("The count is " + count); Outputs the sting literal

More information

Control Statements Review CSC 123 Fall 2018 Howard Rosenthal

Control Statements Review CSC 123 Fall 2018 Howard Rosenthal Control Statements Review CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Review some of Java s control structures Review how to control the flow of a program via selection mechanisms Understand if, if-else,

More information

Fundamentos de programação

Fundamentos de programação Fundamentos de programação Orientação a Objeto Classes, atributos e métodos Edson Moreno edson.moreno@pucrs.br http://www.inf.pucrs.br/~emoreno Contents Object-Oriented Programming Implementing a Simple

More information

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Chapter 6 SINGLE-DIMENSIONAL ARRAYS Chapter 6 SINGLE-DIMENSIONAL ARRAYS Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk What is an Array? A single array variable can reference

More information

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS These are sample pages from Kari Laitinen s book "A Natural Introduction to Computer Programming with Java". For more information, please visit http://www.naturalprogramming.com/javabook.html CHAPTER 5

More information

What we have seen so far CSC 142. Object based programming in Java [Reading: chapter 4] Another example. java.awt. Initializing instance fields

What we have seen so far CSC 142. Object based programming in Java [Reading: chapter 4] Another example. java.awt. Initializing instance fields CSC 142 Object based programming in Java [Reading: chapter 4] CSC 142 C 1 What we have seen so far A class is made of instance fields: attributes of the class objects instance methods: behavior of the

More information

Object-oriented programming in...

Object-oriented programming in... Programming Languages Week 12 Object-oriented programming in... College of Information Science and Engineering Ritsumeikan University plan this week intro to Java advantages and disadvantages language

More information

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology Computer Science II OO Programming Classes Scott C Johnson Rochester Institute of Technology Outline Object-Oriented (OO) Programming Review Initial Implementation Constructors Other Standard Behaviors

More information

Section 2.2 Your First Program in Java: Printing a Line of Text

Section 2.2 Your First Program in Java: Printing a Line of Text Chapter 2 Introduction to Java Applications Section 2.2 Your First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler are denoted using a. Two

More information

Lab5. Wooseok Kim

Lab5. Wooseok Kim Lab5 Wooseok Kim wkim3@albany.edu www.cs.albany.edu/~wooseok/201 Question Answer Points 1 A 8 2 A 8 3 E 8 4 D 8 5 20 5 for class 10 for main 5 points for output 6 A 8 7 B 8 8 0 15 9 D 8 10 B 8 Question

More information

Section 2.2 Your First Program in Java: Printing a Line of Text

Section 2.2 Your First Program in Java: Printing a Line of Text Chapter 2 Introduction to Java Applications Section 2.2 Your First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler are denoted using a. Two

More information

OBJECT ORİENTATİON ENCAPSULATİON

OBJECT ORİENTATİON ENCAPSULATİON OBJECT ORİENTATİON Software development can be seen as a modeling activity. The first step in the software development is the modeling of the problem we are trying to solve and building the conceptual

More information