Objects. Getting classy Your current job –Gain experience creating and manipulating objects from the standard Java types Why –Prepares you for defining.

Slides:



Advertisements
Similar presentations
Computer Programming Lab(7).
Advertisements

CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
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.
Using Objects CS 101-E Aaron Bloomfield. Announcements Midterm 1 is a week from this Wednesday Midterm 1 is a week from this Wednesday TESTS ARE IN CHM.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
1 Using Objects Chapter 3 Fall 2005 CS 101 Aaron Bloomfield.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
1 Using Objects Chapter 2 (part 2 of 2) Spring 2007 CS 101 Aaron Bloomfield.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Java basics. Task  Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943.
1 Using Objects Chapter 3 Fall 2006 CS 101 Aaron Bloomfield.
1 Even even more on being classy Aaron Bloomfield CS 101-E Chapter 4+
Lecture 7: Whiles - Indefinite Loops Input and while loops Katie Coons CS 305J February 2, 2007.
Introduction to Programming
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Iteration Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Iteration. Java looping Options –while –do-while –for Allow programs to control how many times a statement list is executed.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
1 Using Objects Chapter 3 Spring 2006 CS 101 Aaron Bloomfield.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Lab 8. Declaring and Creating Arrays in One Step datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10];
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Java basics – part 2. Where are we Last week –Java basics Simple output program Started programs as calculators –Codelab –Lab procedures This week –Types,
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.6 Reading Input with java.io Produced by Harvey.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Introduction to programming in java
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Chapter 3 Spring 2005 CS 101 Aaron Bloomfield
CSC111 Quick Revision.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Objects.
Elementary Programming
Chapter 2 Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Maha AlSaif Maryam AlQattan
Java basics – part 3.
Using Objects.
Data types, Expressions and assignment, Input from User
Something about Java Introduction to Problem Solving and Programming 1.
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
CSE 1020:Programming by Delegation
Introduction to Computer Science and Object-Oriented Programming
בתרגול הקודם אתר הקורס (הודעות, פרטי סגל הקורס, עבודות, פורום, מערכת הגשת תרגילים וכו') שימוש בחלון ה-command, פקודות בסיסות קוד Java: הידור (= קומפילציה)
class PrintOnetoTen { public static void main(String args[]) {
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Chapter 3 Spring 2006 CS 101 Aaron Bloomfield
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Announcements & Review
Objects.
Methods/Functions.
More on iterations using
Optional Topic: User Input with Scanner
Presentation transcript:

Objects

Getting classy Your current job –Gain experience creating and manipulating objects from the standard Java types Why –Prepares you for defining your own classes and creating and manipulating the objects of those classes

Values versus objects Numbers –Have values but they do not have behaviors Objects –Have attributes and behaviors System.in References an InputStream –Attribute: keyboard –Behaviors: reading System.out –References an OutputStream Attribute: monitor Behaviors: printing

Some other Java object types Scanner String Rectangle Color JFrame

Consider Statements int peasPerPod = 8; String message = "Don't look behind the door!“ How show we represent these definitions according to the notions of Java?

Representation

Shorthand representation

Quick survey I am starting to get a warm and fuzzy feeling about objects versus vales a.Pretty much b.With a little review, I’ll have it down c.Not really d.I’m so lost I have done the readings a.True b.False

Examples Consider String a = "excellence“; String b = a; What is the representation?

Examples Consider String a = "excellence“; String b = a; What is the representation?

Uninitialized versus null Consider String dayOfWeek; Scanner inStream; What is the representation?

Uninitialized versus null Consider String dayOfWeek; Scanner inStream; What is the representation?

Uninitialized versus null Consider String fontName = null; Scanner fileStream = null; What is the representation?

Uninitialized versus null Consider String fontName = null; Scanner fileStream = null; What is the representation?

Assignment Consider String word1 = "luminous"; String word2 = "graceful"; Word1 = word2; Initial representation

Quick survey I see that null and non-initialized are different concepts a.Pretty much b.With a little review, I’ll have it down c.Not really d.I’m so lost I have done the readings a.True b.False

Assignment Consider String word1 = "luminous"; String word2 = "graceful"; Word1 = word2; After assignment

Using objects Consider Scanner stdin = Scanner.create(System.in); System.out.print("Enter your account name: "); String response = stdin.nextLine();

Using objects Consider Scanner stdin = Scanner.create(System.in); System.out.print("Enter your account name: "); String response = stdin.nextLine();

Using objects Consider Scanner stdin = Scanner.create(System.in); System.out.print("Enter your account name: "); String response = stdin.nextLine(); Suppose the user interaction is Enter your account name: artiste

String representation Consider –String alphabet = "abcdefghijklmnopqrstuvwxyz"; Standard shorthand representation Truer representation

String representation Consider –String alphabet = "abcdefghijklmnopqrstuvwxyz"; –char c1 = alphabet.charAt(9); –char c2 = alphabet.charAt(15); –char c3 = alphabet.charAt(2); What are the values of c1, c2, and c3? Why?

Program WordLength.java public class WordLength { public static void main(String[] args) { Scanner stdin = Scanner.create(System.in); System.out.print("Enter a word: "); String word = stdin.nextLine(); int wordLength = word.length(); System.out.println("Word " + word + " has length " + wordLength + "."); }

More String methods Consider String weddingDate = "August 21, 1976"; String month = weddingDate.substring(0, 6); System.out.println("Month is " + month + "."); What is the output?

More String methods Consider String weddingDate = "August 21, 1976"; String month = weddingDate.substring(0, 6); System.out.println("Month is " + month + "."); What is the output? Month is August.

More String methods Consider String fruit = "banana"; String searchString = "an"; int n1 = fruit.indexOf(searchString, 0); int n2 = fruit.indexOf(searchString, n1 + 1); int n3 = fruit.indexOf(searchString, n2 + 1); System.out.println("First search: " + n1); System.out.println("Second search: " + n2); System.out.println("Third search: " + n3); What is the output?

More String methods Consider String fruit = "banana"; String searchString = "an"; int n1 = fruit.indexOf(searchString, 0); int n2 = fruit.indexOf(searchString, n1 + 1); int n3 = fruit.indexOf(searchString, n2 + 1); System.out.println("First search: " + n1); System.out.println("Second search: " + n2); System.out.println("Third search: " + n3); What is the output? First search: 1 Second search: 3 Third search: -1

Quick survey Strings look like they are going to be fun a.Pretty much b.With a little review, I’ll have it down c.Not really d.I’m so lost I have done the readings a.True b.False

More String methods Consider int v1 = -12; double v2 = 3.14; char v3 = 'a'; String s1 = String.valueOf(v1); String s2 = String.valueOf(v2); String s3 = String.valueOf(v3);

Final variables Consider final String POEM_TITLE = “Appearance of Brown"; final String WARNING = “Weather ball is black"; What is the representation?

Final variables

Rectangle

Consider final Rectangle BLOCK = new Rectangle(6, 9, 4, 2); BLOCK.setLocation(1, 4); BLOCK.resize(8, 3);

Rectangle Consider final Rectangle BLOCK = new Rectangle(6, 9, 4, 2); BLOCK.setLocation(1, 4); BLOCK.resize(8, 3);

Final variables Consider final String LANGUAGE = "Java";