CS 121 – Intro to Programming:Java - Lecture 2 Announcements Course home page: First Owl assigment.

Slides:



Advertisements
Similar presentations
Classes And Objects II. Recall the LightSwitch Class class LightSwitch { boolean on = true; boolean isOn() { return on; } void switch() { on = !on; }
Advertisements

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.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Lecture 1: Comments, Variables, Assignment. Definitions The formal (human-readable) instructions that we give to the computer is called source code The.
Written by: Dr. JJ Shepherd
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
Classes and Objects in Java
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 20, 2004 Last update:
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
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:
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
Writing Classes (Chapter 4)
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Lecture 2: Classes and Objects, using Scanner and String.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
What does a computer program look like: a general overview.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
CS 121 – Intro to Programming:Java - Lecture 5 Announcements Course home page: Owl due Thursday at 11; another one up today. Third programming assignment.
Variables, Primitives, and Objects A Visual Learner’s Guide.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Classes and Objects in Java
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
SEEM Java – Basic Introduction, Classes and Objects.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Written by: Dr. JJ Shepherd
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
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.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
OOP Basics Classes & Methods (c) IDMS/SQL News
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
GC211 Data structure Lecture 3 Sara Alhajjam.
Crash course in the Java Programming Language
Multiple variables can be created in one declaration
Introduction to Computer Programming
Type Conversion, Constants, and the String Object
Defining Your Own Classes Part 1
Introduction to Primitive Data types
Java’s Central Casting
Methods/Functions.
Introduction to Primitive Data types
Presentation transcript:

CS 121 – Intro to Programming:Java - Lecture 2 Announcements Course home page: First Owl assigment is up, due Friday. Second assignment up soon. First programming assignment now up due in class, on paper, next Tuesday Recommended IDE - jGRASP It comes with the disk in your textbook (in windows format however). I’ll make public some additional notes shortly on how get jGRASP, where to put it, etc.

Design Goals of the Programming Enterprise Correctness Efficiency Implementation Goals Robustness - what happens when you type with your elbows Adaptability - what happens when people start actually using your beautiful program - and they want to make changes to it Reusability - How well can the pieces of what you’ve written be repackaged for future use

Java’s Object Model Classes - a class is a blueprint or template for an object an Object is an example or an instantiation of a class definition an instance variable or attribute is a characteristic of an Object a method is an activity that serves an object Example: A Student class is a template for a student Attributes - name, age, credits, year, etc Methods - getAge, creditsLeft, calcGPA, and so forth Writing a class definition prepares this template Different students mean different objects -- with different attribute values!

Hospital class attributes? methods? Bank Account class attributes? methods? Person class Baseball player class Mother class

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); } }

Primitive Data Types objects are Java’s main currency Too tedious for them to be the only currency - making numbers a kind of object is a pain, however. Primitive data types integers (4), floats(2), char, boolean. That these aren’t actually objects will turn out to be a pain, too. Statement like these are fairly common: int count = 0; boolean okSoFar = false; char averageGrade = ‘C’; // note the single quotes Details in section 2.4

Operator Precedence Pretty important, but not very interesting num = / 3; not the same as num = (2 + 8) / 3; *, / evaluated first, unless overridden by parentheses. Moral: use parentheses pretty much always… Make sure you read up on 2.4, 2.5, 2.6 for Thursday!

Strings A very important class. String greeting; greeting = new String(“ola”); greeting2 = new String(“howdy”); greeting = greeting2; System.out.println(greeting); Some caveats: 1)Strings are not primitives (unlike float, double, int, etc) 2)Strings are so important that there’s a shorthand for String creation: greeting = “ola”; // works fine 3) Cell model for variables is different: now a cell hold reference to location of (actual) object -- rather than the object itself

As we’ve seen, classes/objects are served by methods Certainly true for Strings String pupName = “spot”; int len = pupName.length(); char what = pupName.charAt(1); char what = pupName.charAt(0); String huh = pupName.concat(“less”); String bigHuh = pubName.toUpperCase();

A word on +: String noise = “moo”; int age = 20; age = age + age; noise = noise + noise; noise = 20+noise; noise = noise; noise = noise+20+30;

Let’s mess with jGRASP, and some changes to OldMacdonald -change “woof” to “bark” --add a dotted line divider before each verse