COMP-202 More Complex OOP

Similar documents
Warm up question. 2)Which of the following operators are defined on a half integer? Which of the following would it be useful to write as behaviors?

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

COMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional)

Garbage Collection (1)

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

COMP-202 Unit 4: Programming with Iterations

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Object Oriented Programming. Java-Lecture 1

Example: Computing prime numbers

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

The User Experience Java Programming 3 Lesson 2

public static void main(string args[]){ arraylist(); recursion();

AP Computer Science Unit 1. Programs

4. Java Project Design, Input Methods

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

Primitive vs Reference

Introduction to Java Unit 1. Using BlueJ to Write Programs

AP Computer Science Unit 1. Writing Programs Using BlueJ

CPSC 219 Extra review and solutions

Slide 1 CS 170 Java Programming 1

COMP 110/L Lecture 5. Kyle Dewey

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Last Class. While loops Infinite loops Loop counters Iterations

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

Dynamic Programming. See p of the text

Methods CSC 121 Spring 2017 Howard Rosenthal

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Accurate study guides, High passing rate! Testhorse provides update free of charge in one year!

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Chap. 3. Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L,

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

COMP 202 Java in one week

APCS Semester #1 Final Exam Practice Problems

CS 211: Existing Classes in the Java Library

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

AP Computer Science Unit 1. Writing Programs Using BlueJ

CS1083 Week 2: Arrays, ArrayList

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

Java Classes - Using your classes. How the classes you write are being used

A variable is a name for a location in memory A variable must be declared

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Warmup : Name that tune!

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

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Math Modeling in Java: An S-I Compartment Model

Chapter 1 Introduction to Java

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

A Quick and Dirty Overview of Java and. Java Programming

Inheritance and Interfaces

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Midterms Save the Dates!

Text User Interfaces. Keyboard IO plus

Tips from the experts: How to waste a lot of time on this assignment

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Program Fundamentals

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

COMP-202 Unit 5: Basics of Using Objects

Computer Science 210 Data Structures Siena College Fall 2018

We now start exploring some key elements of the Java programming language and ways of performing I/O

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

Lecture 8 " INPUT " Instructor: Craig Duckett

Methods CSC 121 Fall 2016 Howard Rosenthal

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Final Exam CS 251, Intermediate Programming December 10, 2014

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

Java and OOP. Part 2 Classes and objects

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

What did we talk about last time? Examples switch statements

CSCI 1103: File I/O, Scanner, PrintWriter

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Over and Over Again GEEN163

St. Edmund Preparatory High School Brooklyn, NY

Computer Science is...

Final CSE 131B Spring 2004

Tips from the experts: How to waste a lot of time on this assignment

Chapter Two Bonus Lesson: JavaDoc

Introduction to JAVA

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Lab5. Wooseok Kim

Hi everyone. Starting this week I'm going to make a couple tweaks to how section is run. The first thing is that I'm going to go over all the slides

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

Last Class. More on loops break continue A bit on arrays

Package. A package is a set of related classes Syntax to put a class into a package: Two rules: Example:

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

CS121: Computer Programming I

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements

CLASSES AND OBJECTS. Fundamentals of Computer Science I

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Code Listing H-1. (Car.java) H-2 Appendix H Packages

Simple Java Reference

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Transcription:

COMP-202 More Complex OOP

Defining your own types: Remember that we can define our own types/classes. These classes are objects and have attributes and behaviors

You create an object or an instance of a class, by using the new operator. type variablename = new type( [parameters]) parameters can be there or not, depending on how you wrote the constructor

Motive to create our own type: Let's say we have a desire in our program to use a type that isn't built in. One solution is to use an enum However, sometimes this will not work.

Creating a huge int: Suppose we have a program that needs to store a very large number. This happens a lot in cryptography. We could try using a long, but this may not be sufficient in some cases. On Friday, we started to create our own class called LongLong, which will treat 2 longs as 1

In order to make a LongLong useful, we will have to do things like define how to add, subtract, multiply, and divide them together.

public static void main(string[] args){ //LongLong l = 11111232312312312313123; //we can't do the above! LongLong big= new LongLong(12345678910, 12345678910); //we want this to store the number //1234567891012345678910 which is too long //to fit into a normal long (continued)

} LongLong big2= new LongLong(12345678910, 12345678910); // LongLong big3 = big1 + big2; // we can't do the above either LongLong big3 = big1.add(big2); // OR LongLong big3 = LongLong.add(big1,big2);

To do this, we will store 2 numbers as long long firstpart; long secondpart; Some useful methods: add() subtract() multiply() print()

In addition, we need someway to set the two parts from outside of the class. We can do this in a constructor that that takes two longs as arguments. We also may want to add getfirstpart(); getsecondpart(); setfirstpart(); setsecondpart();

For adding, we add things digit by digit. Note that we have to be very careful when we add things to avoid a number being larger than the largest possible int. L1 ---> stores as firstpart = 1 secondpart = 2 L2 ----> stores as firstpart 2 secondpart = 3 Result of L1+L2 : firstpart = 3, secondpart=5

But what if the secondpart already stores the biggest possible int? Then this will cause a problem. For example, suppose the biggest int is 100 for simplicitiy. L1: firstpart = 50, secondpart = 60 L2: firstpart = 10, secondpart = 50 60 + 50 is bigger than 100!

Steps to adding 2 LongLong: 1)Start with the right most digit of the second part 2)Add the 2 digits. Store the carry result into a variable carry (could be zero) 3)Add the next 2 digits together and add the result carry 4)Continue until you reach end of secondpart 5)Do the same for firstpart

In base 10, to get the nth digit of a number x, we can use the following: x % 10^(n) / 10^(n-1) In binary, we would use the same except with 2 instead of n. x % 2^(n) / 2^(n-1)

Ex: Suppose we are in base-10 still and the biggest number we can store is 99 LongLong x1 : 33 58 LongLong x2: 03 24

Ex: Suppose we are in base-10 still and the biggest number we can store is 99 LongLong x1 : 33 58 LongLong x2: 03 24 36 82 --> LongLong total 8+4 = 2, set carry = 1 5+2 + carry = 8, set carry = 0 3+3 + carry = 6, set carry = 0 3+0 + carry = 3, set carry = 0

To implement this on a computer, we must do the following: define a variable carry, initialize to 0 For every digit n from the right until the left do the following: define x = nth digit of 1 st number define y = nth digit of 2 nd number if (x+y) is 2 digits, then set carry = 1 else carry = 0 total = total + (x+y)%2 * 2^n

We will have to do this in 2 separate for loops. One loop to compute the secondpart of our new LongLong One loop to computer the firstpart of our new LongLong

Exercise at home: Try this with subtraction. It will be very similar, but remember the method for borrowing from the next column. Hint: Have something like the following int borrow = 0; for (...) { if (firstdigit borrow seconddigit < 0) { } }

Once you have written the subtract method, it will be very easy to deal with negative numbers. There are 4 cases: a,b both positive (we wrote this) 1 is negative, one is positive (we can write in terms of subtract) both are negative: we can write in terms of -1 * the normal addition

Using a LongLong as part of a more complex object: Once we have define the type LongLong, we can use it as any other type. This includes using it as part of the definition of another type!

} public class LongLongVector { private LongLong x; private LongLong y; private LongLong z;...

public class LongLongVector { pubic LongLongVector(LongLong x, LongLong y, LongLong z) { } this.x = x; this.y = y; this.z = z; } Because LongLong is a reference, this does NOT make a copy of the values stored

The significance of this is you have now created an alias between the two. Suppose somewhere (i.e. in a main method) you write: LongLong x = new LongLong(1,2); LongLong y = new LongLong(5,6); LongLong z = new LongLong(8,9); LongLongVector llvector= new LongLongVector(x,y,z); x.setfirstpart(4);

The significance of this is you have now created an alias between the two. Suppose somewhere (i.e. in a main method) you write: LongLong x = new LongLong(1,2); LongLong y = new LongLong(5,6); LongLong z = new LongLong(8,9); LongLongVector llvector= new LongLongVector(x,y,z); x.setfirstpart(4); --->Changes llvector too!!!!

public class LongLongVector { pubic LongLongVector(LongLong x, LongLong y, LongLong z) { this.x = new LongLong(x.getFirstPart(), x.getsecondpart()); this.y = new LongLong(y.getFirstPart(), y.getsecondpart()); this.z = new LongLong(z.getFirstPart(), z.getsecondpart()); } } Because getfirstpart() and getsecondpart() return primitive types, we now have copied everything over and don't need to worry about duplicates

Another option is to not have a setfirstpart() or setsecondpart() method. This makes it impossible to ever change a LongLong after you create it. In fact, this is how Strings work. Important distinction: LongLong longlong = new LongLong(3,4); longlong = new LongLong(6,7); /*This does not change the LongLong. Rather, it creates a brand new LongLong */

Another option is to not have a setfirstpart() or setsecondpart() method. This makes it impossible to ever change a LongLong after you create it. In fact, this is how Strings work. Important distinction: LongLong longlong = new LongLong(3,4); longlong = new LongLong(6,7); /*This does not change the LongLong. Rather, it creates a brand new LongLong */

Packages We have seen in Java that: A group of commands makes a method A group of methods makes a class A package is a group of classes

Packages There are many packages that come with Java standard library. java.lang general (includes String and System) java.util utilities (really! with that name!?) -- (includes Scanner) java.io input and output classes (for writing to files) java.awt and java.swing GUI classes

Packages There are 2 ways you can use a class that is part of a package: The first is to use the entire, fully qualified name java.util.scanner scan = new java.util.scanner(system.in); This works because the Scanner class is part of the java.util package. If you do this, you do not have to write import java.util.scanner! (Yeah, dude it's totally awesome!)

Packages When you import a package with an import statement, you tell Java to consider that package as well when looking for classes. So if you write import java.util.scanner; Then when it sees the token Scanner it will scan the package java.util for it Note: java.lang.* is automatically imported even if you don't write anything.

Analyzing System.out.println() System is a class inside of the java.lang package out is a public static attribute of the System class. static ---> write the name of a class before the. public --->means I can use it outside attribute---> in general, you can only write an attribute or a behavior after the dot. Since we write a. AFTER out as well, then it must be an attribute out is actually an object itself. This object, we are calling the println() method on.

Example: ArrayList Are you sick of having to resize arrays by copying values one at a time? Tired of manually searching for values in an array? Well...then use ArrayList!

Example: ArrayList ArrayList<int> foo = new ArrayList<int>(); foo.add(3); System.out.println( The array size now is + foo.size()); foo.add(4); System.out.println( The array size now is + foo.size()); System.out.println( 3 is located at position + foo.indexof(3));

Using command line arguments Ever wondered what the deal with String[] args is? For the most part, we have been running our programs by typing java NameOfClassWithMainMethod However, you can also type java NameOfClassWithMainMethod some other words or 123 numbers When you do this, the array args is initialized to values { some, other, words, or, 123 }

Using command line arguments Why would I want to do this? One potential motivation is to run your program automatically but with slightly different inputs each time.

Using command line arguments public class CommandLineArgEcho { public static void main(string[] args) { System.out.println(args[0]); } } /*now when I run my program, I'll get different results*/ java CommandLineArgEcho 5 ---> prints 5 java CommandLineArgEcho hello -----> prints hello

Using command line arguments This is better than reading from the keyboard because we can line up a whole bunch of calls to a program in a row. It is possible, for example, using a batch or bat file to run your program hundreds of times in a row with different inputs

Using command line arguments In the preceding example, what do you think would happen if you wrote: java CommandLineArgsEcho (without anything afterwards)?

Using command line arguments Array out of bounds error!