CS 200 Using Objects Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
Types CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 4, Horstmann text)
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
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.
String and Scanner CS 21a: Introduction to Computing I First Semester,
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Exam Review 10/01/2014 Happy October. The Exam  Will be in Canvas  Two parts  Part A is recall – closed book, closed notes ◦Quizzes, in class activity.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Yanal Alahmad Java Workshop Yanal Alahmad
Methods.
CS Week 2 Jim Williams, PhD.
CS 200 Branches Jim Williams, PhD.
CS Week 4 Jim Williams, PhD.
Comp Sci 302 Introduction to Programming
CMSC 202 Static Methods.
CS Week 8 Jim Williams, PhD.
Starting Out with Java: From Control Structures through Objects
CS Week 6 Jim Williams, PhD.
CS Week 7 Jim Williams, PhD.
Type Conversion, Constants, and the String Object
CS Week 3 Jim Williams, PhD.
CS 302 Week 8 Jim Williams, PhD.
CS Week 9 Jim Williams, PhD.
Week 6 CS 302 Jim Williams, PhD.
CS 200 Objects and ArrayList
Java so far Week 7.
Classes and Objects 5th Lecture
IFS410 Advanced Analysis and Design
Fundamentals 2.
AP Java Review If else.
CS 200 More Primitives, Objects, Branches and Methods
CS 200 Loops Jim Williams, PhD.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
CS 200 Primitives and Expressions
CS 200 Arrays Jim Williams, PhD.
CS Week 4 Jim Williams, PhD.
CS 200 Primitives and Expressions
CS 200 Methods, Using Objects
AP Java Review If else.
Classes and Objects Static Methods
Outline Creating Objects The String Class The Random and Math Classes
Names of variables, functions, classes
CS Week 2 Jim Williams, PhD.
Building Java Programs
Chapter 2: Java Fundamentals cont’d
Happy October Exam Review 10/01/2014.
CSS161: Fundamentals of Computing
CS Week 3 Jim Williams, PhD.
Week 7 CS 302 Jim Williams.
Ben Stanley for gAlpha gALPHA free, four-week venture-creation workshop designed to help entrepreneurially-minded students and technologists create high-growth.
References Revisted (Ch 5)
CS 200 Objects and ArrayList
CS302 Week 6 Jim Williams.
Presentation transcript:

CS 200 Using Objects Jim Williams, PhD

This Week Notes Lecture: Using Objects By Friday Exam Conflict and Accommodations Install Eclipse (version 8) Help Queue Team Lab 2 Chap 2 Programs (P2): Due Thursday Hours Spent Week? Lecture: Using Objects

Review of Key Concepts in Methods Call Definition Parameter, Parameter list Argument Header Body Return data type, value Variable Scope

Is this mPrint Call or Definition? static void mPrint() { System.out.println("my print"); } B is the correct answer

Defining Methods public class M { //method definition static void mPrint() { System.out.println("my print"); } public static void main(String []args) { mPrint(); // method call. B is the correct answer

Argument vs. Parameter static void printCount(int count) { //parameter public static void main(String []args) { int num = 10; printCount( 23); //argument is value passed in printCount( num+3); } static void printCount(int count) { //parameter System.out.println( count); count is a parameter (zyBooks) or formal parameter the number 23, for example is an argument (also called an actual parameter).

Local Variables Local variables are those declared within a method. Variables declared within a method are only available within that method.

What prints out? public static void calc(int num) { num = num + 3; } 5 35 error public static void calc(int num) { num = num + 3; } public static void main(String []args) { int num = 5; calc( num); System.out.println( num); try it.

Returning a Value from a Method public static void main(String []args) { int value = 5; int result = triple( value); } public static int triple(int num) { return num * 3;

Which is called first: calc or println? error public static int calc(int num) { num -= 33; return num; } public static void main(String []args) { int n = 55; System.out.println( calc( n)); put a print statement within the calc method to see if it is called before the println method.

Using Objects Lots of existing Java code! How do we build off it rather than having to write it ourselves? API - Application Programming Interface

Calling Class (static) Methods double numInts = Math.pow( 2, 32); double root = Math.sqrt( 16); int num1 = 16; int num2 = 3; double result; result = num2 + Math.sqrt( num1); API

Instance vs. Class (static) Methods method definition has “static” modifier use name of class when calling Math.max( 10, 20); Instance (non-static) Methods method definition does Not have “static” modifier use instance of class when calling Random randGen = new Random(); randGen.nextInt( 5);

Primitive vs Reference Data Types Store value int i = 5; 5 i Reference Store a reference to another location in memory that contains value Integer num; num = new Integer(9); num 9

Thought Experiment Imagine: Someone asks you to teach an introductory programming course this summer. Are you preparing yourself? Are you simply looking for an "A" or are you preparing yourself to teach programming to others?

The Study Cycle Check Am I using study methods that are effective? Do I understand the material enough to explain it to others? http://students.lsu.edu/academicsuccess/studying/strategies/tests/studying

What kind of methods? max: class nextInt: instance Math.max( 10, 20); max: instance nextInt: class Math.max( 10, 20); Random randGen = new Random(); randGen.nextInt( 5); A is the best answer

Wrapper Classes Wrapper classes contain helpful methods. Primitive type Wrapper class boolean Boolean byte Byte char Character float Float int Integer long Long short Short double Double

Data Type Conversions String weekNum = 3; String weekNum = "" + 3; String numStr = Integer.toString( 4); int num = Integer.parseInt( numStr);

Wrapper Classes Boxing: Putting primitive into instance of wrapper class Unboxing: retrieve primitive from instance auto-boxing/unboxing: when compiler inserts code to box/unbox. Primitive type Wrapper class boolean Boolean byte Byte char Character float Float int Integer long Long short Short double Double

Primitive vs Reference Variables int num1; num1 = 5; Integer num2; num2 = new Integer(7); Integer num3 = 9; int num4 = num3; //use Java Visualizer, options Show String/Integer objects checkbox //use Java Visualizer, options Show String/Integer objects checkbox

What will show when d1 is printed? Double d1 = new Double(10); double d2 = d1; d1 = 14.1; Double d3 = d1; d1 = d2; System.out.println( d3); 10 14.1 a reference error yes. try it.

String class A reference (class), not a primitive data type. Frequently used final String TITLE_PREFIX = "Welcome to Week "; int week = 2; System.out.println( TITLE_PREFIX + week);

String Alex Johnson alex johnson String name2 = "Alex"; error String name2 = "Alex"; name2.toLowerCase(); String name3 = name2 + " Johnson"; System.out.print( name3); instances of String are immutable (cannot change) methods in String class that appear to change strings actually return new strings.

Calling String methods aChar: 'i' aChar: 's' aChar: ' ' String strA = "This is a string"; char aChar = strA.charAt( 3);

What is the answer? AP? String s1 = "An important programming tool."; String s2 = s1.substring( 9, 10); String s4 = new String( "?"); if ( s1.contains( "gram")) { s4 = s1.substring( 2, 4).trim(); } char c3 = s1.charAt( s1.indexOf('g') -3); String answer = (s2 + c3 + s4).toUpperCase(); AP? api ANP IM API try it.

Read In Values Recall: import java.util.Scanner; Scanner input = new Scanner( System.in); int age = input.nextInt(); String name = input.nextLine();

What are values of variables? String note = "1 2\nAlex two words"; Scanner input = new Scanner( note); int num1 = input.nextInt(); int num2 = input.nextInt(); String name = input.next(); String words = input.nextLine();

What are values of variables? name: Minsub\n age: 22\nCS major: name: Minsub\n22\CS age: name: Minsub age: 22 String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); String major = input.nextLine();

What are values of variables? name: Minsub\n22\CS age: major: name: Minsub age: 22 name: Minsub major: CS String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); String major = input.nextLine();

What are values of variables? score1: 20 score2: 25 title: scores title: score1: 20 25 score2: scores String note = "20 25\nscores"; Scanner input = new Scanner( note); int score1 = input.nextInt(); int score2 = input.nextInt(); String title = input.nextLine();

java.util.Random Random randGen; //Declare reference variable randGen = new Random(); //create instance // randGen.setSeed( 123); //set state int valueA = randGen.nextInt( 5); //get a value int valueB = randGen.nextInt( 5); int valueC = randGen.nextInt( 5);

What is the answer? x = rand.nextInt(10)-2; What expression to get a value between 2 and 10, inclusive of both 2 and 10? Assume: Random rand = new Random(); int x; x = rand.nextInt(10)-2; x = rand.nextInt(9)+1; x = rand.nextInt(8)+2; x = rand.nextInt(9)+2; Other try it.

Debugging with Print statements See what is going on. Divide and conquer. String s1 = "An important programming tool."; String s2 = s1.substring( 9, 10); String s4 = new String( "?"); if ( s1.contains( "gram")) { s4 = s1.substring( 2, 4).trim(); } char c3 = s1.charAt( s1.indexOf('g') -3); String answer = (s2 + c3 + s4).toUpperCase();