CS 121 – Intro to Programming:Java - Lecture 5 Announcements Course home page: Owl due Thursday at 11; another one up today. Third programming assignment.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

The ArrayList Class and the enum Keyword
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CS 121 – Intro to Programming:Java - Lecture 2 Announcements Course home page: First Owl assigment.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Chapter 4: Writing Classes
Mock test review Revision of Activity Diagrams for Loops,
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Intro to CS – Honors I Classes and Methods GEORGIOS PORTOKALIDIS
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: Owl due Friday;
CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:
CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
CS 121 – Intro to Programming:Java - Lecture 7 Announcements A new Owl assignment is available. Programming assignment 4 is due on Thursday - hand in on.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 121 – Intro to Programming:Java - Lecture 8 Announcements Current Owl assignment is due Wednesday. Next Owl assignment - arrays I - will be up by tomorrow.
Programming in Java (COP 2250) Lecture 9 Chengyong Yang Fall, 2005.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Announcements CLUE Tutoring Wednesday nights 7-8:30PM MGH extra-credit points for each session you attend from last week through the end of the quarter.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Chapter 4: Writing Classes. 2 b We've been using predefined classes. Now we will learn to write our own classes to define new objects b Chapter 4 focuses.
Java – Methods Lecture Notes 5. Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address,
CS 121 – Intro to Programming:Java - Lecture 12 Announcements New Owl assignment up soon (today?) For this week: read Ch 8, sections 0 -3 Programming assignment.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Lecture 4: For Loops Announcements & Review Announcements Examples posted after lecture on schedule Discussion Section exercises on line before 9am Tuesdays.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
CS 121 – Intro to Programming:Java - Lecture 11 Announcements Inheritance Owl assignment due 11/30 at 11 am Programming assignment six due Friday 12/3.
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
GC211 Data structure Lecture 3 Sara Alhajjam.
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Writing Classes We have talked about classes and objects.
ECS10 10/10
Classes A class is a blueprint of an object
Chapter 4: Writing Classes
CS 302 Week 11 Jim Williams, PhD.
Introduction to Computer Programming
Class Structure 16-Nov-18.
Chapter 5 – Writing Classes
Class Structure 28-Nov-18.
Review for Final Exam.
Outline Boolean Expressions The if Statement Comparing Data
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
CS 200 Loops Jim Williams, PhD.
CS 200 Primitives and Expressions
Class Structure 25-Feb-19.
Scope of variables class scopeofvars {
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Methods/Functions.
What to expect this week
First Semester Review.
More on iterations using
Presentation transcript:

CS 121 – Intro to Programming:Java - Lecture 5 Announcements Course home page: Owl due Thursday at 11; another one up today. Third programming assignment due on friday, Read chapter 4 through 4.5 for today, on mid-term. Midterm review link up soon. No honors section tomorrow MidTerm: Monday, 10/18, , Thompson 102,106 Office hours - now posted; held in LGRT 220

The state of affairs: So far most of our attention has been focused on Java at the statement level: what do statements do, and how can we string them together to get a job done (e.g.calculate compound interest) Today we take a serious look at the object model in Java We’ve already done some of this informally..

A class definition in Java functions as a template for object. A definition, however, is not an object. It only tells how to make one, and what actions are available to service one, once it’s been made. Two main elements: attributes (describe state of an object) methods (describe possible behaviors) A good example - a radio

public class Radio { boolean on = false; boolean fm; // false means am! float station; public Radio(boolean onFM; float curStation){ fm = onFM; station = curStation; } public void turnOff() {on = false;} public void setStation(float newStation){station = newStation;} public boolean amNow(){return (!fm);} }

Radio radio1 = new Radio(true, 88.5); Radio radio2 = new Radio(false, ); radio1.turnOff();

œ’÷◊public class Coin{ œœß œœßœÌœprivate final int HEADS = 0; œœßœÌœprivate final int TAILS = 1; œœß œœßœÌœprivate int face; œœß œœßœfifl‡public Coin(){ œœßœœ®ππœflip(); œœßœœ©} œœß œœßœfifl‡public void flip(){ œœßœœ®ππœface = (int)(Math.random()*2); œœßœœ©} œœß œœßœfifl

‡public boolean isHeads(){ œœß¬πƒππœreturn(face == HEADS); œœßœœ©} œœß œœßœfifl‡public String toString(){ œœßœœ®πÌœString faceName; œœßœœ®π≥¥if (face == HEADS) œœßœœßœ6æππœfaceName = "heads"; œœßœœßœˆπelse faceName = "tails"; œœß¬πƒππœreturn faceName; œœßœœ©} œœ©}

œ’÷◊public class CoinTest1{ œœß œœßœfifl‡public static void main(String[] args){ œœßœœß œœßœœ®πÌœCoin c = new Coin(); œœßœœ®ππœc.flip(); œœßœœ®ππœSystem.out.println(c); œœßœœ®ππœc.flip(); œœßœœ®ππœSystem.out.println(c); œœßœœ©} œœ©} œ´œ ----jGRASP exec: java CoinTest1 œœßœheads œœßœtails œœßœœœ©œ ----jGRASP: operation complete.

œ’÷◊public class CoinTest2{ // toss two coins - how many trials before two heads show up?œœß œœßœfifl‡public static void main(String[] args){ œœßœœß œœßœœ®πÌœCoin c1 = new Coin(); œœßœœ®πÌœCoin c2 = new Coin(); œœßœœ®πÌœint count = 0; œœßœœ®ππ±while(!c1.isHeads() || !c2.isHeads()){ œœßœœßœœ7ππœcount++; œœßœœßœœ7ππœc1.flip(); œœßœœßœœ7ππœc2.flip(); œœßœœßœœ∞} œœßœœ®ππœSystem.out.println(count); œœßœœ©} œœ©} > typical answer: 4

œ’÷◊public class CoinTest3{ // how many flips are needed before 6 heads in row are found?// h œœßœfifl‡public static void main(String [] args){ œœßœœ®πÌœint count = 0; œœßœœ®πÌœint target = 6; œœßœœ®πÌœint headsSoFar = 0; œœßœœ®πÌœCoin c = new Coin(); œœßœœ®ππ±while (headsSoFar < target){ œœßœœßœœ7ππœcount++; œœßœœßœœ7ππœc.flip(); œœßœœßœœ7π≥πif (c.isHeads()) headsSoFar++; œœßœœßœœ5œˆπelse headsSoFar = 0; œœßœœßœœ∞} œœßœœ®ππœSystem.out.println("flips to " + target + " is " + count); œœßœœ©} œœ©}

œœ œœ´œ ----jGRASP exec: java CoinTest3 œœßœflips needed to 6 is 81 œœßœ œœ©œ ----jGRASP: operation complete. œœœœ œœ´œ ----jGRASP exec: java CoinTest3 œœßœflips needed to 6 is 403 œœßœ œœ©œ ----jGRASP: operation complete. Some sample data

A new method for the Coin class // does “flips” number of coin tosses, returns # of heads public int headCount(int flips){ int ct = 0; for (int j = 0; j < flips; j++){ flip(); if (isHeads()) ct++; } return ct; }

Another method for the Coin class // returns true if the state of a coin is tails public boolean isTails(){ return (!isHeads()); }

Write a method called maybeSwap. It is passed two strings as parameters, and prints them out in the given order with a space in between - unless the second string is longer. If that happens, print them out in the reverse order: second string first.

public void maybeSwap(String r, String s){ if (r.length() < s.length()) System.out.println(s + “ “ + r); else System.out.println(r + “ “ + s); }

Big ideas Class syntax - the legal pattern of symbols Scope - who can see a variable Methods and return statements - subprograms Method parameters - adjusting method behavior Local data - data used in a local context Constructors - makes an object Method overloading - different signatures for methods Method decomposition - using several methods to get a job done.

Old MadDonald does Java Old Macdonald had a farm // first two lines make chorus œœßœei, ei, o; œœßœand on that farm he had a pig œœßœei ei O œœßœWith an oink oink here œœßœAnd a oink oink there œœßœHere a oink there a oink œœßœEverywhere a oink oink œœßœOld Macdonald had a farm œœßœei, ei, o; œœßœOld Macdonald had a farm œœßœei, ei, o; and on that farm he had a dog œœßœei ei O (etc…)

Overall structure Chorus Pig verse Chorus Dog verse Chorus Who are the players? Chorus object Verse object - it’s parameterized (pig, dog, etc) A coordinating “song” class

public class MacSong{ public static void main(String[] args){ MacChorus m = new MacChorus(); MacVerse p = new MacVerse("pig", "oink"); MacVerse d = new MacVerse("dog", "woof"); m.chorus(); p.verse(); m.chorus(); d.verse(); m.chorus(); } } Three objects made - a chorus object, and two verse objects. The verse objects differ in that their attributes hold different values. The verse method exploits this to give different verses.

public class MacChorus{ public void chorus(){ // a method that serves MacChorus System.out.println("Old Macdonald had a farm"); System.out.println("ei, ei, o;"); } }

public class MacVerse{ String name; String noise; // attributes public MacVerse(String animalName, String animalNoise){ name = animalName; noise = animalNoise; } public String getName(){return name;} public String getNoise(){return noise;}; public void verse(){ System.out.println("and on that farm he had a " + name); System.out.println("ei ei O"); System.out.println("With a " + noise + " " + noise + " here"); System.out.println("And a " + noise + " " + noise + "there"); System.out.println("Here a " + noise + " there a " + noise); System.out.println("Everywhere a " + noise + " " + noise); } }

public void verse(){ System.out.println("and on that farm he had a " + name); System.out.println("ei ei O"); System.out.println("With a " + noise + " " + noise + " here"); System.out.println("And a " + noise + " " + noise + "there"); System.out.println("Here a " + noise + " there a " + noise); System.out.println("Everywhere a " + noise + " " + noise); } A proposed alteration: System.out.println("With " + addArticle(noise) + " " + noise + " here");

public boolean isVowel(char c){ return((c == ‘a’) || (c == ‘e’) || (c == ‘i’) || (c == ‘o’) || (c == ‘u’) ); } public String addArticle(String s){ char c = s.charAt(0); if (isVowel(c) ) return (“an “ + s); else return (“a “ + s); }