Darrell Bethea May 20, 2011

Similar documents
Loop - 3 & Class. Cheng, Wei COMP May 22, Title

COMP Introduction to Programming Midterm Review

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

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

by Pearson Education, Inc. All Rights Reserved. 2

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015

Fundamentals of Programming Data Types & Methods

( &% class MyClass { }

Darrell Bethea May 25, 2011

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

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Supplementary Test 1

CSC 1051 Data Structures and Algorithms I

CS111: PROGRAMMING LANGUAGE II

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Introduction to Computers and Java

CS1150 Principles of Computer Science Methods

Introduction to Java (All the Basic Stuff)

Lab5. Wooseok Kim

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

CS111: PROGRAMMING LANGUAGE II

CS159. Nathan Sprague

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CSC 1051 Data Structures and Algorithms I

Programming with Java

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Midterm Examination (MTA)

Sri Vidya College of Engineering & Technology

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Methods and Data (Savitch, Chapter 5)

Maximization and Minimization Problems. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Introduction to Computer Science Unit 2. Notes

Lab5. Wooseok Kim

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

CS1150 Principles of Computer Science Methods

Building Java Programs

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

Assignment 1 due Monday at 11:59pm

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

MIDTERM REVIEW. midterminformation.htm

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Computer Programming, I. Laboratory Manual. Final Exam Solution

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

CMPT 125: Lecture 4 Conditionals and Loops

Classes. Classes as Code Libraries. Classes as Data Structures

BSc (Hons) Computer Science with Network Security/ BSc (Hons) Software Engineering/ BSc (Hons) Web Technologies. Examinations for 2016 Semester 1

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Week 2: Data and Output

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

CS 139 Practice Midterm Questions #2

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

COMP 110 Introduction to Programming. What did we discuss?

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Introduction to Computers and Java

Introduction to Computers and Java

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

Object Oriented Design

Week 6 CS 302. Jim Williams, PhD

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

CSC 1051 Data Structures and Algorithms I

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

5. PACKAGES AND INTERFACES

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

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

Introduction to Computers and Java

Methods. Eng. Mohammed Abdualal

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

Anatomy of a Java Program: Comments

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Exam Duration: 2hrs and 30min Software Design

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Algorithms and Conditionals

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

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

Full file at

JAVA: A Primer. By: Amrita Rajagopal

Java Primer 1: Types, Classes and Operators

SDKs - Eclipse. SENG 403, Tutorial 2

CPSC 219 Extra review and solutions

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

Creating Classes and Objects

Unit 4: Classes and Objects Notes

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

CS121: Computer Programming I

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

PROGRAMMING FUNDAMENTALS

Lecture 5: Methods CS2301

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

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

Intro to Programming in Java Practice Midterm

Object-Oriented Programming Concepts

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Transcription:

Darrell Bethea May 20, 2011

Program 2 due Monday Midterm in one week Moved to SN014 (next door) Will cover up to yesterday (Ch. 1-4) Future office hours moved to FB331 2

3

Briefly go over Strings and Loops Worksheet Classes 4

num++ is NOT num + 1 num++ does num = num + 1 So does ++num, BUT, there is a difference int num1 = 5; System.out.println(num1++); // outputs num1 (5), then increments num1 int num2 = 5; System.out.println(++num2); // increments num2, then outputs num2 (6) 10

Java programs (and programs in other objectoriented programming languages) consist of objects of various class types Objects can represent objects in the real world Automobiles, houses, employee records Or abstract concepts Colors, shapes, words 11

A class is the definition of a kind of object A blueprint for constructing specific objects Class Name: Automobile Data: amount of fuel speed license plate Methods (actions): accelerate: How: Press on gas pedal. decelerate: How: Press on brake pedal. 12

Object Name: patscar amount of fuel: 10 gallons speed: 55 miles per hour license plate: 135 XJK Object Name: suescar amount of fuel: 14 gallons speed: 0 miles per hour license plate: SUES CAR Instantiations, or instances, of the class Automobile Object Name: ronscar amount of fuel: 2 gallons speed: 75 miles per hour license plate: 351 WLF 13

- fuel: double - speed: double - license: String Automobile + accelerate(double pedalpressure): void + decelerate(double pedalpressure): void Class name Data Methods (actions) 14

Important: classes do not have data; individual objects have data Classes specify what kind of data objects have 15

Each Java class definition goes in its own, SEPARATE.java file ClassName save the file as ClassName.java Student.java includes the class Student 16

What happens when you compile a.java file?.java file gets compiled into a.class file Contains Java bytecode Same filename except for.class instead of.java You can compile a Java class before you have a program that uses it 17

- Name - Year - GPA - Major - Credits - GPA sum Class Name: Student + getname + getmajor + printdata + increaseyear How: increase year by 1 + calcgpa How: average grades 18

Class Name: Student - name: String - year: int - gpa: double - major: String - credits: int - gpasum: double + getname(): String + getmajor(): String + printdata(): void + increaseyear(): void + calcgpa(double grade): void 19

public class Student { public String name; public int classyear; public double GPA; public String major; //... Class name Data (instance variables) } public String getmajor() { return major; } public void increaseyear() { classyear++; } Methods 20

Create an object jack of class Student Student jack = new Student(); Assign memory address of object to variable Return memory address of object Create an object Scanner keyboard = new Scanner(System.in); Create an object keyboard of class Scanner 21

Data defined in the class are called instance variables public String name; public int classyear; public double GPA; public String major; variables public: no restrictions on how these instance variables are used (more details later public is actually a bad idea here) type: int, double, String 22

public class Student { public String name; public int classyear; public double GPA; public String major; //... public String getmajor() { return major; } } public void increaseyear() { classyear++; } 23

public static void main(string[] args) { Student jack = new Student(); jack.name = Jack Smith ; jack.major = Computer Science ; System.out.println(jack.name + is majoring in + jack.major); Student apu = new Student(); apu.name = Apu Nahasapeemapetilon ; apu.major = Biology ; } System.out.println(apu.name + is majoring in + apu.major); jack.name and apu.name are two different instance variables because they belong to different objects 24

Two kinds of methods Methods that return a value Examples: String s.substring() method, String s.indexof() method, etc. Methods that return nothing Example: System.out.println() 25

returns a String public String getmajor() { return major; } public void increaseyear() { classyear++; } return type returns nothing 26

Method heading: keywords public: no restriction on how to use the method (more details later) void: the method returns nothing Method body: statements executed when the method is called (invoked) Must be inside a pair of braces public void increaseyear() { classyear++; } 27

As usual, inside a block (defined by braces), you can have multiple statements public void printdata() { System.out.println( Name: + name); System.out.println( Major: + major); System.out.println( GPA: + gpa); } 28

Syntax: object name. method name () -- with arguments inside, if any Use them as Java statements Student jack = new Student(); jack.classyear = 1; jack.increaseyear(); System.out.println( Jack s class year is + jack.classyear); 29

Method heading: keywords public: no restriction on how to use the method (more details later) Type: the type of value the method returns Method body: statements executed Must be inside a pair of braces Must have a return statement public String getmajor() { return major; } 30

A method that returns a value must have at least one return statement Terminates the method, and returns a value to the caller Syntax: return Expression; Expression can be any expression that produces a value of type specified by the return type in the method heading 31

public String getclassyear() { if (classyear == 1) return Freshman ; else if (classyear == 2) return Sophomore ; else if... } 32

object, followed by dot, then method name, then () (same as before) Use them as a value of the type specified by the method s return type Student jack = new Student(); jack.major = Computer Science ; String m = jack.getmajor(); System.out.println( Jack s full name is + jack.getname()); System.out.println( Jack s major is + m); 33

Can also be used in methods that return nothing Terminates the method Syntax: return; public void increaseyear() { if (classyear >= 4) return; classyear++; } 34

Monday More about classes Program 2 due 35 35