CS 302 Week 9. Jim Williams

Similar documents
CS Week 13. Jim Williams, PhD

CS Week 14. Jim Williams, PhD

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CS 200 More Classes Jim Williams, PhD

Programming II (CS300)

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

CS1004: Intro to CS in Java, Spring 2005

CS 200 Arrays, Loops and Methods Jim Williams, PhD

CS Week 5. Jim Williams, PhD

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Unit 5: More on Classes/Objects Notes

COE318 Lecture Notes Week 10 (Nov 7, 2011)

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Introduction to Programming Using Java (98-388)

Java Review. Fundamentals of Computer Science

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Recitation 3 Class and Objects

CS 101 Exam 2 Spring Id Name

Programming II (CS300)

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Programming II (CS300)

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Chapter 4 Defining Classes I

CS1083 Week 2: Arrays, ArrayList

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

CS 101 Fall 2006 Midterm 3 Name: ID:

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

CS 251 Intermediate Programming Methods and More

Principles of Object Oriented Programming. Lecture 4

CS 200 Command-Line Arguments & Exceptions Jim Williams, PhD

Programming II (CS300)

Software and Programming 1

Classes. Classes as Code Libraries. Classes as Data Structures

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

This exam is open book. Each question is worth 3 points.

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)

ITI Introduction to Computing II

CS 200 Objects and ArrayList Jim Williams, PhD

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Objects as a programming concept

Practice Midterm 1. Problem Points Score TOTAL 50

CS 251 Intermediate Programming Methods and Classes

CS121/IS223. Object Reference Variables. Dr Olly Gotel

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

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

CS 101 Spring 2006 Final Exam Name: ID:

ITI Introduction to Computing II

AP COMPUTER SCIENCE A

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

CSC Java Programming, Fall Java Data Types and Control Constructs

Instance Method Development Demo

Week 6 CS 302. Jim Williams, PhD

Anatomy of a Class Encapsulation Anatomy of a Method

Enum Types. Built-in support for types of discrete values Advantages over C++ s enum: Type-safety Body can include methods and fields

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

CSCE145 Test 2-Review 03/29/2015 Hongkai Yu

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

Programming II (CS300)

Notes on Chapter Three

Java and OOP. Part 2 Classes and objects

Chapter 4: Writing Classes

Chapter 6 Lab Classes and Objects

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Getting started with Java

Ticket Machine Project(s)

Lecture 2: Java & Javadoc

Assignment 1 due Monday at 11:59pm

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

I. True/False: (2 points each)

CS 113 MIDTERM EXAM 2 SPRING 2013

Computer Science II (20082) Week 1: Review and Inheritance

Classes Classes 2 / 35

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

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

AP CS Unit 4: Classes and Objects Programs

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

CSCI 1103: Object-Oriented Objects

I. True/False: (2 points each)

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Chapter 9 Enumerations

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

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

CS Week 11. Jim Williams, PhD

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Media Computation. Lecture 15.2, December 3, 2008 Steve Harrison

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Lecture 5: Methods CS2301

Class, Variable, Constructor, Object, Method Questions

CMSC131. Library Classes

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

public static boolean isoutside(int min, int max, int value)

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

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

University of Palestine. Mid Exam Total Grade: 100

Transcription:

CS 302 Week 9 Jim Williams

This Week P2 Milestone 3 Lab: Instantiating Classes Lecture: Wrapper Classes More Objects (Instances) and Classes Next Week: Spring Break

Will this work? Double d = new Double(10); double d2 = d; yes no sometimes error

Is result true or false? Integer m = 5; Integer n = 5; boolean result = m == n; true false error Java Language Specification https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html

Classes and Objects various variable declarations accessors (getters),mutators (setters) constructor, no-arg, default public, private, protected, <package> state of an object constructor overloading this

Memory areas code static heap stack

toppings is a(n) class Pizza { private String toppings; public String gettoppings() { if ( toppings == null) toppings = "no toppings"; return toppings; instance variable class variable parameter local variable

The visibility modifier private is class Pizza { private String toppings; public String gettoppings() { if ( toppings == null) toppings = "no toppings"; return toppings; appropriate should be public doesn't allow a user of this class to change the field (attribute) directly allows only methods within this class to change the field.

A static field means class Pizza { static String toppings; public String gettoppings() { if ( toppings == null) toppings = "no toppings"; return toppings; every instance will have the exact same toppings toppings is a class variable can be accessed by any method cannot be changed

gettoppings() is a(n) class Pizza { private String toppings; public String gettoppings() { if ( toppings == null) toppings = "no toppings"; return toppings; method getter accessor provides read-only access to toppings field

gettoppings() class Pizza { private String toppings; public static String gettoppings() { if ( toppings == null) toppings = "no toppings"; return toppings; is a class method can access toppings cannot access toppings will not compile

State of an instance The current values of its attributes. Changing the values changes the state.

Ball Example class Ball { boolean inflated; //main Ball b = new Ball(); b.inflated = true; Ball b2 = new Ball();

settoppings class Pizza { private String toppings; public void settoppings( String tops) { if ( tops!= null && tops.length() > 0) toppings = tops; is a class method can change toppings (write access) is a setter is a mutator

this The way to access the current instance from within the instance methods.

constructor Called when instance created Used to initialize instance fields

Can a setter also get? class Pizza { private String toppings; public String settoppings( String tops) { String previous = toppings; toppings = tops; return previous; yes no depends error

Object/Instance State enum Color {GREEN,YELLOW, RED; class TrafficLight{ private Color color = Color.RED; public void change() { if ( color == Color.GREEN) color = Color.YELLOW; else if ( color == Color.YELLOW) color = Color.RED; else color = Color.GREEN; public Color getcolor() { return color; public String tostring() { return color.name();

What variables are used for state in "instance who's reference is in p"? class Person { private static int count = 0; private boolean amhungry = false; public Person( String name) { public static void main(string []args) { Person p = new Person("Fred"); count name amhungry amhungry name amhungry count amhungry

Visibility Modifiers For members of a class: public private protected <package> Demo

Can methoda call methodb? //classes in different files in same package class A { public void methoda() { B b = new B(); b.methodb(); class B { void methodb() { yes no depends error

Can a method outside the package call methoda()? //classes in different files in same package class A { public void methoda() { B b = new B(); b.methodb(); class B { void methodb() { yes no depends error

Bike numwheels numgears

Will tostring return results of Graphic tostring()? class Bird { private Graphic graphic; Bird(String name) { this.graphic = new Graphic(name); public String tostring() { return this.graphic.tostring(); yes no NullPointerException other

this class Picture { private boolean hasframe; public Picture( boolean hasframe) { this.hasframe = hasframe;

Does this print true or false? class Person { static int count = 0; private boolean something = false; boolean getthing(boolean something) { return this.something; Person p = new Person(); System.out.println( p.getthing( true)); true false error/other

Does this print 0, 1, other or error? class Person { static int count = 0; private boolean something = false; Person(boolean something) { this.something = something; count++; System.out.println( Person.count); 0 1 other error