Class. Chapter 6: Data Abstraction. Example. Class

Size: px
Start display at page:

Download "Class. Chapter 6: Data Abstraction. Example. Class"

Transcription

1 Chapter 6: Data Abstraction In Java, there are three types of data values primitives arrays objects actually, arrays are a special type of object Class In Java, objects are used to represent data values Just as 3 is a primitive value of type int, every object must have a type These types are called classes Class A class defines a type of object, including its data: the information that is used to represent the object the operations (methods) that can be performed on the data After the type is defined, objects of that type can be defined and used An individual value of a class is called an object, or an instance of the class Example Suppose we want to represent planets in Java We can define a class Planet, with data: diameter, mass, distance from the sun, orbital period, rotational period, location,... methods: setdiameter, setmass, etc. getdiameter, getmass, etc. move Instances of Planet might include earth, mars, venus, saturn

2 Objects and Methods Different objects have different methods for manipulating their data The specific methods are determined based on what makes sense for that type of object For example, with strings: length, concatenation, comparison, substring String: A standard class String is a standard Java class Remember, values from a class are called objects, so "hello" is an object from the class String, or an instance of the class The class String has instance methods that operate on an instance of the class For example: length(), concat(), compareto(), charat() Methods Each type of object supports a specified set of methods There are two types of methods in a class instance methods class methods Instance Methods Instance methods are called for a specific object and have direct access to that object s data without having to pass the object as a parameter String s = "abc"; s.length(); The object is implicitly passed as a parameter to the method. We use the object's name followed by a '.', followed by the method name

3 String Instance Methods boolean equals( Object anobject ) compares this String with another object not the same as '==' See StringCompare.java int length() Number of characters in this String char charat( int index ) The character at position index in this String index ranges from 0 to length() - 1 IndexOutOfBoundsException String Instance Methods 2 int compareto(string str) Returns an integer value, based on lexicographic order int indexof(int ch) Index of where the first occurrance of ch occurs in this string or -1 if not present int indexof(string str) Index of the first character of a matching substring str or -1 if not present String Instance Methods 3 String concat(string str) Concatenates this string instance with str and returns the result String tolowercase() Returns a copy of this string, but converted to all lower case String touppercase() Returns a copy of this string, but converted to all upper case Class Methods These methods are part of the class definition, but don't operate on a specific instance of the class Class methods are indicated by the keyword static We have written class methods up to now Class methods provide some type of operation related to the objects provided by the class Math.random() Math.sqrt()

4 String.valueOf() static String valueof( type prim ) returns the String representation of the value of prim valueof is an overloaded function valueof( char c ) valueof( char[] data ) valueof( int i ) valueof( double d ) String.valueOf() static String valueof( type prim ) We don't need an instance of String to call this method Instead, we use the class name: String s; int value = 1234; s = String.valueOf( value ); System.out.println( s.length() ); StringTest.java // StringTest.java - demo some String methods public class StringTest { public static void main(string[] args) { String str1 = "abcd", str2 = "abcd", str3; System.out.println(str1.equals(str2)); System.out.println(str1.length()); System.out.println(str1.charAt(1)); System.out.println(str1.compareTo("aBcE")); System.out.println(str1.compareTo("aBcC")); System.out.println(str1.compareTo("aBcD")); System.out.println(str1.indexOf('D')); System.out.println(str1.indexOf("Bc")); System.out.println(str1.indexOf("zz")); // parts cut out here StringTest.java // StringTest.java - demo some String methods public class StringTest { public static void main(string[] args) { String str1 = "abcd", str2 = "abcd", str3; // parts cut out here System.out.println(str1.concat("efg")); str3 = str1.tolowercase(); System.out.println(str3); str3 = str1.touppercase(); System.out.println(str3); System.out.println(str1); str3 = String.valueOf(123); System.out.println(str3.equals("123"));

5 String Literals String literals are instances of String Therefore, we can invoke instance methods on them "abc".length() "hello".charat( 1 ); String is unusual Because strings are so common, Java provides some special syntax for the class String special support for concatenation special support for String literals String is the only class that is treated specially String Concatenation The '+' operator is overloaded to support String concatenation "hello " + "world" is equivalent to "hello ".concat( "world" ); String Literals String literals are supported String s = "hello"; is equivalent to char[] temp={ h, e, l, l, o ; String s = new String(temp); Instance methods are operations on objects

6 Strings are immutable Instances of class String are immutable Once its value is assigned, it cannot be changed We can create a new string, but we can't change the one we have. One implication of this is that when we pass a String to a method String s = "hello"; somemethod( s ); StringBuffer StringBuffer is another standard Java class for representing strings It has instance methods that allow you to change the value of a StringBuffer object These are called mutator methods insert(), reverse(), replace(), setcharat(), deletecharat(),... we know that its value will be unchanged when the method returns s will have the value "hello" when somemethod returns StringBufferTest.java // StringBufferTest.java - demo StringBuffer methods public class StringBufferTest { public static void main(string[] args) { StringBuffer sbuf1 = new StringBuffer(); StringBuffer sbuf2 = new StringBuffer("abcd"); StringBuffer sbuf3 = new StringBuffer(30); System.out.println(sbuf1.length()); System.out.println(sbuf2.length()); System.out.println(sbuf3.length()); System.out.println(sbuf1.capacity()); System.out.println(sbuf2.capacity()); System.out.println(sbuf3.capacity()); System.out.println(sbuf2.charAt(1)); // parts cut out here StringBufferTest.java // StringBufferTest.java - demo StringBuffer methods public class StringBufferTest { public static void main(string[] args) { sbuf2.setcharat(2,'z'); System.out.println(sbuf2); sbuf2.append("xyz"); System.out.println(sbuf2); sbuf2.append('?'); sbuf2.insert(4, "---"); sbuf2.insert(2, '+'); System.out.println(sbuf2); sbuf2.reverse(); System.out.println(sbuf2); System.out.println("sbuf2 capacity " + sbuf2.capacity()); System.out.println("sbuf2 length " + sbuf2.length());

7 Object Declarations When you declare an object, you are really creating a reference Similar to array declaration and allocation StringBuffer sbuf1 = new StringBuffer(); StringBuffer sbuf2 = new StringBuffer("abcd"); StringBuffer sbuf3 = new StringBuffer(30); Can also do this with separate statements StringBuffer sbuf1; sbuf1 = new StringBuffer(); Elements of a Simple Class As discussed earlier, a class describes the data values that make up an object along with the operations that can be applied to the object The data values are stored in instance variables, which are also known as fields or data members. The operations are described by instance methods, which are sometimes called procedure members. Example: Counter We often want to count things, why not create an abstraction for doing it? Advantage: you can reuse it in different places in the program, or even in other programs Data: Current value of the counter (initially zero) Operations: Reset, Increment, Decrement, Get the current value CounterTest.java class CounterTest { public static void main( String[] args ) { Counter countthis = new Counter(); Counter countthat = new Counter(); countthis.increment(); countthis.increment(); countthat.increment(); System.out.println("countThis: " + countthis.get() ); System.out.println("countThat: " + countthat.get() );

8 class Counter { int value; Counter.java Objects in Memory void reset() { value = 0; void increment() { value++; void decrement() { value--; int get() { return value; No main() method value is an instance variable Instance methods no static keyword CountThis CountThat Local variables in main() Counter objects value 2 value 1 Class Counter reset() increment() decrement() get() Important Details Each Counter object has its own copy of the member variables In this case, the integer variable called value When the methods are called, the call is of the form <objectname>.<methodname>() The object itself is an implicit parameter to the method, so that any references to the data access that object s copy of the instance variables Abstract Data Type Counter is an example of an Abstract Data Type (ADT) an abstraction representing a particular type of data Classes allow us to implement ADTs The data and methods combine to implement the functionality we desire or expect for this type of data The implementation details are hidden from the user The implementation is all in one place The type can be used in many different places in the program or in many programs

9 Package In general, each class is in a separate file The name of the file should match the class name (with.java at the end) All classes in the same directory are part of the same package Whether or not a method is in the same class or package as the data or method it is accessing affects what it can see and do. Data Hiding It is desirable to hide the inner details of a class (ADT) from the users of the class We want to be able to determine the correctness of our class without having to examine the entire program that it is used in For example, with our Counter class, we want to ensure that the value doesn't change by more than 1. Data Hiding Accessing instance variables from outside the class violates the data hiding principle class CounterTest2 { public static void main( String[] args ) { Counter c = new Counter(); c.value = 150; System.out.println( "c.value = " + c.get() ); A Better Counter Class Use the private and public access specifiers to control access to instance variables and methods class Counter { private int value; public void reset() { value = 0; public void increment() { value++; public void decrement() { value--; public int get() { return value;

10 Public/Private/Default private methods/fields cannot be accessed from outside of the class public methods/fields can be accessed from anywhere default (no specifier) methods/fields have package access they can be accessed from other classes in the same package if you don't specify a package (see section 12.11), all classes in the same directory are part of the same default, un-named package Constructing Objects We create or construct objects with the new operator StringBuffer sb = new StringBuffer(); Counter countthis = new Counter(); This allocates memory for the object and initializes the object Constructing Objects An object is initialized by calling its constructor A constructor is a special method in a class It has the same name as the class No return type is specified Implicit return type is instance of class It is automatically called when an object of that type is created Constructors are usually used to set data in the object to an initial value Constructors can take parameters Constructing Objects If the class definition does not include a constructor, then Java uses a default, noargument constructor This initializes all instance variables booleans to false other primitives to 0 everything else to null

11 Counter: Another Look This class does not have a defined constructor The default, no-argument constructor initializes the value field to 0 It is invoked when new Counter() is executed class Counter { private int value; public void reset() { value = 0; public void increment() { value++; public void decrement() { value--; public int get() { return value; Adding Constructors to Counter Want to create a Counter with an initial value class Counter { private int value; public Counter() { value = 0; public Counter( int v ) { value = v; public void reset() { value = 0; public void increment() { value++; public void decrement() { value--; public int get() { return value; Using Constructors Create two counters using our constructors class CounterTest3 { public static void main( String[] args ) { Counter countthis = new Counter(10); Counter countthat = new Counter(); countthis.increment(); countthis.increment(); countthat.increment(); System.out.println("countThis: " + countthis.get() ); System.out.println("countThat: " + countthat.get() ); Constructor Error Java provides the default, no-argument constructor only if there are no constructors defined in the class. If we had only provided this constructor public Counter( int v ) { value = v; Then creating a Counter with Counter countthat = new Counter(); would be a syntax error, because there is no constructor that takes 0 arguments.

12 Another Example Complex Numbers, with real and imaginary parts public class Complex { private double real; private double imaginary; public Complex() { real = 0; imaginary = 0; public Complex(double r, double i) { real = r; imaginary = i; Another Example Make some complex numbers Complex a = new Complex(); Complex b = new Complex( 3.7, 5.1 ); Complex c = new Complex( 3, 0 ); public double getreal() { return real; public double getimaginary() { return imaginary; import java.util.random; A Dice Example import tio.*; Testing the Dice class Dice { private int die1, die2; private Random roller; Dice(int seed) { roller = new Random(seed); void roll() { die1 = roller.nextint(6) + 1; die2 = roller.nextint(6) + 1; int gettotal() { return die1 + die2; public String tostring() { return die1 + ", " + die2; class DiceTest { public static void main(string[] args) { System.out.println("Enter the seed."); Dice dice = new Dice(Console.in.readInt()); System.out.println("How many times should I roll?"); int count = Console.in.readInt(); while(count > 0) { dice.roll(); System.out.println("You rolled " + dice); System.out.println("The total is " + dice.gettotal()); count--;

13 A Person Class Testing the Person Class class Person { private String name; private int age; private char gender; public Person( String n, int a, char g ) { name = n; age = a; gender = g; public String getname() { return name; public int getage() { return age; public char getgender() {return gender; class PersonTest { public static void main( String[] args ) { Person bill = new Person( "Bill", 25, 'M' ); Person amy = new Person( "Amy", 33, 'F' ); printperson( bill ); printperson( amy ); static void printperson( Person p ) { System.out.println( "Name: " + p.getname() ); System.out.println( "Age: " + p.getage() ); System.out.println( "Gender: " + p.getgender() ); tostring() You can use System.out.println() to display any object However, the default behavior is not very useful You can write your own tostring() method for your classes public String tostring() By providing every class with a tostring() method, we can use System.out.println() to print ANY object value class Person { private String name; private int age; private char gender; Person.toString() public Person( String n, int a, char g ) { name = n; age = a; gender = g; public String tostring() { return "Name: " + name + "\nage: " + age + "\ngender: " + gender;

14 Using tostring() class PersonTest2 { public static void main( String[] args ) { Person bill = new Person( "Bill", 25, 'M' ); Person amy = new Person( "Amy", 33, 'F' ); System.out.println( bill ); System.out.println( amy ); Static Methods and Variables Remember, a static (or class) method is one that is not associated with an object Static methods are invoked using the class name Math.random() String.valueOf() Similarly, a static or class variable is one that is independent of the objects of the class Static Variables Class variables are associated with the class rather than with a particular instance of the class As with static methods, public static variables are accessed using the class name followed by the variable name Math.PI A static variable is created once, when the class is first encountered by Java. How Many Counters? Add a static variable to keep track of the number of Counter objects class Counter { private int value; private static int howmany = 0; public Counter() { value = 0; howmany++; public Counter( int v ) { value = v; howmany++; public void reset() { value = 0; public void increment() { value++; public void decrement() { value--; public int get() { return value; public static int howmany() { return howmany;

15 How Many Counters? Class Variables in Memory class CounterTest4 { public static void main( String[] args ) { Counter countthis = new Counter(10); Counter countthat = new Counter(); countthis.increment(); countthis.increment(); countthat.increment(); System.out.println("countThis: " + countthis.get() ); System.out.println("countThat: " + countthat.get() ); System.out.println("How Many: " + Counter.howMany() ); CountThis CountThat Local variables in main() Counter objects value 12 value 1 Class Counter 2 howmany reset() increment() decrement() get() howmany() Let's put it all together Write a class Clock that represents a 24 hour clock Midnight = 0 This class should have a constructor that takes three parameters: hour, minute, and second Include the following instance methods: add( Clock c ) add c to a Clock instance For example, add 1:00:00 to 9:30:00 tostring() - format c as "HH:MM:SS" Let's put it all together Access instance variables should be accessible only from the class instance methods should be accessible from everywhere

16 Class Constants It is generally a bad idea for instance or class variables to be public It is better to provide accessor and mutator methods This allows us to guarantee certain conditions about the data However, there is one type of class data that is commonly made public: Constants Immutable variables with special values Class Constants and Final Examples Math.PI Integer.MAXINT Constants generally written in UPPER CASE Defined with the keyword final public static final double PI = ; Any attempt to modify the value of a constant will result in an error. Why Use Constants? Constants are only defined once Some numbers, such as pi, are frequently used Constants allow us to name a value This makes the code more clear Which is easier to understand? 12 or MONTHSPERYEAR Clock constants Let's add some class constants to the Clock class SECONDSPERMINUTE MINUTESPERHOUR HOURSPERDAY

17 Calling Methods There are three ways to call a method, depending on whether or not the method is in the same class whether the method is an instance method or a class method Calling Methods in the Same Class If a method is in the same class as the calling method, just use the method name a = foo( b ); Instance Instance Instance Class Class Class Class cannot call Instance Why not? Calling Methods in Another Class From another class: To call an instance method, you must use the name of an instance of the class String s = "abc"; int a = s.length(); To call a class method, you must use the name of the class double r = Math.random(); Calling Methods Let's add a method tick() to our clock class Advance the time 1 second Call another method in Clock to implement this method.

18 Accessing Another Object's Private Variables An instance method operating on one object can access another object's private fields Remember, private limits variable access to methods in the class The add() method of Clock does this Clock when = new Clock( 15, 30, 0 ); Clock howlong = new Clock( 1, 0, 0 ); when.add( howlong ); Implementation details are hidden from other classes, but not from objects in the same class Passing Objects to Methods As with Arrays, objects are references So, when we pass an object to a method, we can change its value class PassingReferences { public static void main(string[] args) { StringBuffer sbuf = new StringBuffer("testing"); System.out.println("sbuf is now " + sbuf); modify(sbuf); System.out.println("sbuf is now " + sbuf); static void modify(stringbuffer sb) { sb.append(", 1 2 3"); Object state before sb.append() Object state after sb.append() Variables in main() Variables in modify(sbuf) Variables in main() Variables in modify(sbuf) sbuf sb sbuf sb testing testing, StringBuffer object StringBuffer object

19 Passing Objects to Methods But, you can't change the value of the reference Object state before new class ModifyParameters { public static void main(string[] args) { StringBuffer sbuf = new StringBuffer("testing"); System.out.println("sbuf is now " + sbuf); modify(sbuf); System.out.println("sbuf is now " + sbuf); static void modify(stringbuffer sb) { sb = new StringBuffer("doesn't work"); sbuf Variables in main() Variables in modify(sbuf) sb testing StringBuffer object Object state after new Variables in main() Variables in modify(sbuf) sbuf sb testing, StringBuffer object doesn't work StringBuffer object Accessing Another Object's Private Variables An instance method operating on one object can access another object's private fields Remember, private limits variable access to methods in the class Implementation details are hidden from other classes, but not from objects in the same class Let's create an add() method in Clock Clock when = new Clock( 15, 30, 0 ); Clock howlong = new Clock( 1, 0, 0 ); when.add( howlong );

20 Calling Methods There are three ways to call a method, depending on whether or not the method is in the same class whether the method is an instance method or a class method Calling Methods in the Same Class If a method is in the same class as the calling method, just use the method name a = foo( b ); Instance Instance Instance Class Class Class Class cannot call Instance Why not? Calling Methods in Another Class From another class: To call an instance method, you must use the name of an instance of the class String s = "abc"; int a = s.length(); To call a class method, you must use the name of the class double r = Math.random(); Calling Methods Let's add a method tick() to our clock class Advance the time 1 second Call another method in Clock to implement this method.

21 Scope: Local Variables We have discussed the scope of local variables (ones defined in methods) already A variable's scope extends from the line it is declared until the end of the block it is contained in A formal parameter's scope is the entire method A variable declared in the for loop control has a scope that extends to the end of the for loop This means that variables can't be used before they are declared, or after the end of their scope Scope: Instance and Class Variables The scope rules for instance and class variables are different The scope of instance and class variables is the entire class regardless of where they are declared They can be referred to from any method in the class This means that you can refer to an instance or class variable before you declare it Example: Clock class class Clock { private int hour; private int minute; private int second; public Clock( int h, int m, int s ) { hour = h % HOURSPERDAY; minute = m % MINUTESPERHOUR; second = s % SECONDSPERMINUTE; public Clock() {... public void add( Clock c ) {... public void tick() {... public String tostring() {... Hidden Variables Local variables hide or eclipse instance and class variables with the same name References to that name will refer to the local variable public static final int HOURSPERDAY = 24; public static final int MINUTESPERHOUR = 60; public static final int SECONDSPERMINUTE = 60;

22 Hidden Class Variables Access hidden class variables using the class name //Scope2.java: class versus local scope class Scope2 { public static void main(string[] args) { int x = 2; System.out.println("local x = " + x); System.out.println("class variable x = " + Scope2.x); static int x = 1; Hidden Instance Variables Last time one of the sample question asked us to write a class Point class Point { private int x; private int y; public Point( int x1, int y1 ) { x = x1; y = y1; public String tostring() { return ( x + ", " + y ); Hidden Instance Variables Notice that we named the Constructor's parameters x1 and y1 instead of the more intuitive x and y class Point { private int x; private int y; public Point( int x, int y ) { x = x; // which x is which? y = y; //?... The Keyword this Java provides the keyword this to allow us to refer to an object's hidden instance variables this is a reference to the implicit object class Point { private int x; private int y; public Point( int x, int y ) { this.x = x; this.y = y;...

23 Arrays of Objects Point[] parray = new Point[5]; Just as we can have arrays of primitive types, we can also have arrays of objects Recall that When we declare an array we have to use new to create the storage for the array When we create an object we have to use new to create the storage for the object So, when we create an array of objects we have to use new twice; once for the array and once for the objects. parray The array for (int i = 0; i < parray.length; i++) { parray[i] = new Point(i, i+1); parray x y The array 3 4 x y x y The Point objects x y x y A Deck of Cards The book has an example where a deck of cards is represented as a 52 element array. Each element is an object of type Card //Deck.java - a deck of playing cards class Deck { Card[] deck; Deck() { deck = new Card[52]; for (int i = 0; i < deck.length; i++) deck[i] = new Card(new Suit(i / ), new Pips(i % ));

24 Shuffle the Deck Deck.toString() void shuffle() { for (int i = 0; i < deck.length; i++){ int k = (int)(math.random() * 52); Card t = deck[i]; deck[i] = deck[k]; deck[k] = t; The book has an error The highlighted line is a correction public String tostring() { String t = ""; for (int i = 0; i < 52; i++) if ( (i + 1) % 5 == 0) t = t + deck[i] + "\n"; else t = t + deck[i]; return t; The book has an error The highlighted line is a correction Testing the Deck //CardTest.java testing the shuffling method. public class CardTest { public static void main(string argv[]) { Deck deck = new Deck(); System.out.println("\nNew Shuffle\n" + deck); deck.shuffle(); System.out.println("\nNew Shuffle\n" + deck); deck.shuffle(); System.out.println("\nNew Shuffle\n" + deck);

Class. Chapter 6: Data Abstraction. Example. Class

Class. Chapter 6: Data Abstraction. Example. Class Chapter 6: Data Abstraction In Java, there are three types of data values primitives arrays objects actually, arrays are a special type of object Class In Java, objects are used to represent data values

More information

Objects: Data Abstraction

Objects: Data Abstraction Objects: Data Abstraction In Object-Oriented programming languages like Java, objects are used to represent data A class defines a type of object, including its data its permissible operations Once a type

More information

Chapter 6. Classes revisited. Objects: Data Abstraction. Data Abstraction

Chapter 6. Classes revisited. Objects: Data Abstraction. Data Abstraction Chapter 6 Data Abstraction Classes revisited Classes have several purposes: Classes bundle related stuff together, so far we had one bundle for each program Programs usually use many classes Example: Programming

More information

In Java there are three types of data values:

In Java there are three types of data values: In Java there are three types of data values: primitive data values (int, double, boolean, etc.) arrays (actually a special type of object) objects An object might represent a string of characters, a planet,

More information

Data & Functional Abstraction ( (Parts of Chaps 4 & 6) Objects. Classes. In Java there are three types of data values:

Data & Functional Abstraction ( (Parts of Chaps 4 & 6) Objects. Classes. In Java there are three types of data values: Data & Functional Abstraction ( (Parts of Chaps 4 & 6) In Java there are three types of data values: primitive data values (int, double, boolean, etc.) arrays (actually a special type of object) objects

More information

String. Other languages that implement strings as character arrays

String. Other languages that implement strings as character arrays 1. length() 2. tostring() 3. charat() 4. getchars() 5. getbytes() 6. tochararray() 7. equals() 8. equalsignorecase() 9. regionmatches() 10. startswith() 11. endswith() 12. compareto() 13. indexof() 14.

More information

Lab 14 & 15: String Handling

Lab 14 & 15: String Handling Lab 14 & 15: String Handling Prof. Navrati Saxena TA: Rochak Sachan String Handling 9/11/2012 22 String Handling Java implements strings as objects of type String. Once a String object has been created,

More information

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

2. All the strings gets collected in a special memory are for Strings called  String constant pool. Basics about Strings in Java 1. You can create Strings in various ways:- a) By Creating a String Object String s=new String("abcdef"); b) By just creating object and then referring to string String a=new

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and

More information

Chapter 4: Writing Classes

Chapter 4: Writing Classes Chapter 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Writing Classes We've been using predefined classes. Now we will learn to write our own

More information

Creating Strings. String Length

Creating Strings. String Length Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and

More information

Selected Questions from by Nageshwara Rao

Selected Questions from  by Nageshwara Rao Selected Questions from http://way2java.com by Nageshwara Rao Swaminathan J Amrita University swaminathanj@am.amrita.edu November 24, 2016 Swaminathan J (Amrita University) way2java.com (Nageshwara Rao)

More information

BM214E Object Oriented Programming Lecture 8

BM214E Object Oriented Programming Lecture 8 BM214E Object Oriented Programming Lecture 8 Instance vs. Class Declarations Instance vs. Class Declarations Don t be fooled. Just because a variable might be declared as a field within a class that does

More information

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited Table of Contents Date(s) Title/Topic Page #s 11/6 Chapter 3 Reflection/Corrections 56 Chapter 4: Writing Classes 4.1 Objects Revisited 57 58-59 look over your Ch 3 Tests and write down comments/ reflections/corrections

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

STRINGS AND STRINGBUILDERS. Spring 2019

STRINGS AND STRINGBUILDERS. Spring 2019 STRINGS AND STRINGBUILDERS Spring 2019 STRING BASICS In Java, a string is an object. Three important pre-built classes used in string processing: the String class used to create and store immutable strings

More information

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness. Methods There s a method in my madness. Sect. 3.3, 8.2 1 Example Class: Car How Cars are Described Make Model Year Color Owner Location Mileage Actions that can be applied to cars Create a new car Transfer

More information

More non-primitive types Lesson 06

More non-primitive types Lesson 06 CSC110 2.0 Object Oriented Programming Ms. Gnanakanthi Makalanda Dept. of Computer Science University of Sri Jayewardenepura More non-primitive types Lesson 06 1 2 Outline 1. Two-dimensional arrays 2.

More information

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method Appendix 3 Java - String charat() Method This method returns the character located at the String's specified index. The string indexes start from zero. public char charat(int index) index -- Index of the

More information

STUDENT LESSON A10 The String Class

STUDENT LESSON A10 The String Class STUDENT LESSON A10 The String Class Java Curriculum for AP Computer Science, Student Lesson A10 1 STUDENT LESSON A10 The String Class INTRODUCTION: Strings are needed in many programming tasks. Much of

More information

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto CS 170 Java Programming 1 More on Strings Working with the String class Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 What are Strings in Java? Immutable sequences of 0 n characters

More information

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals 1 Recall From Last Time: Java Program import java.util.scanner; public class EggBasketEnhanced { public static void main(string[]

More information

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Class Encapsulation Anatomy of a Method Writing Classes Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: class definitions instance data encapsulation and

More information

Primitive vs Reference

Primitive vs Reference Primitive vs Reference Primitive types store values Reference types store addresses This is the fundamental difference between the 2 Why is that important? Because a reference type stores an address, you

More information

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014 1 COE 212 Engineering Programming Welcome to Exam I Tuesday November 11, 2014 Instructors: Dr. Bachir Habib Dr. George Sakr Dr. Joe Tekli Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam

More information

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents character strings. All string literals in Java programs,

More information

SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz) SOFTWARE DEVELOPMENT 1 Strings and Enumerations 2018W (Institute of Pervasive Computing, JKU Linz) CHARACTER ENCODING On digital systems, each character is represented by a specific number. The character

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

More information

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All Advanced Java This chapter discusses class String, class StringBuilder and class Character from the java.lang package. These classes provide

More information

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs Chapter 8 User-Defined Classes and ADTs Objectives To understand objects and classes and use classes to model objects To learn how to declare a class and how to create an object of a class To understand

More information

About Codefrux While the current trends around the world are based on the internet, mobile and its applications, we try to make the most out of it. As for us, we are a well established IT professionals

More information

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017 Intro to Strings Lecture 7 COP 3252 Summer 2017 May 23, 2017 Strings in Java In Java, a string is an object. It is not a primitive type. The String class is used to create and store immutable strings.

More information

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations. Data Structures 1 Data structures What is a data structure? Simple answer: a collection of data equipped with some operations. Examples Lists Strings... 2 Data structures In this course, we will learn

More information

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java

More information

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

Operations. I Forgot 9/4/2016 COMPUTER SCIENCE DEPARTMENT PICNIC. If you forgot your IClicker, or your batteries fail during the exam

Operations. I Forgot 9/4/2016 COMPUTER SCIENCE DEPARTMENT PICNIC. If you forgot your IClicker, or your batteries fail during the exam COMPUTER SCIENCE DEPARTMENT PICNIC Welcome to the 2016-2017 Academic year! Meet your faculty, department staff, and fellow students in a social setting. Food and drink will be provided. When: Saturday,

More information

Lecture 5: Methods CS2301

Lecture 5: Methods CS2301 Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int

More information

ECOM 2324 COMPUTER PROGRAMMING II

ECOM 2324 COMPUTER PROGRAMMING II ECOM 2324 COMPUTER PROGRAMMING II Object Oriented Programming with JAVA Instructor: Ruba A. Salamh Islamic University of Gaza 2 CHAPTER 9 OBJECTS AND CLASSES Motivations 3 After learning the preceding

More information

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008) Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008) In previous chapters, methods were called with parameters that were primitive data types. This chapter discusses how to use object

More information

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects Administrative Stuff September 12, 2007 HW3 is due on Friday No new HW will be out this week Next Tuesday we will have Midterm 1: Sep 18 @ 6:30 7:45pm. Location: Curtiss Hall 127 (classroom) On Monday

More information

Java Basic Programming Constructs

Java Basic Programming Constructs Java Basic Programming Constructs /* * This is your first java program. */ class HelloWorld{ public static void main(string[] args){ System.out.println( Hello World! ); A Closer Look at HelloWorld 2 This

More information

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University Chapter 9 Objects and Classes CS1: Java Programming Colorado State University Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops,

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill CSCI 136 Data Structures & Advanced Programming Lecture 3 Fall 2018 Instructors: Bill & Bill Administrative Details Lab today in TCL 217a (and 216) Lab is due by 11pm Sunday Lab 1 design doc is due at

More information

Assignment 1 due Monday at 11:59pm

Assignment 1 due Monday at 11:59pm Assignment 1 due Monday at 11:59pm The heart of Object-Oriented Programming (Now it gets interesting!) Reading for next lecture is Ch. 7 Focus on 7.1, 7.2, and 7.6 Read the rest of Ch. 7 for class after

More information

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All This chapter discusses class String, from the java.lang package. These classes provide the foundation for string and character manipulation

More information

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing Java Programming MSc Induction Tutorials 2011 Stefan Stafrace PhD Student Department of Computing s.stafrace@surrey.ac.uk 1 Tutorial Objectives This is an example based tutorial for students who want to

More information

Chapter 7 User-Defined Methods. Chapter Objectives

Chapter 7 User-Defined Methods. Chapter Objectives Chapter 7 User-Defined Methods Chapter Objectives Understand how methods are used in Java programming Learn about standard (predefined) methods and discover how to use them in a program Learn about user-defined

More information

9 Working with the Java Class Library

9 Working with the Java Class Library 9 Working with the Java Class Library 1 Objectives At the end of the lesson, the student should be able to: Explain object-oriented programming and some of its concepts Differentiate between classes and

More information

Ans: Store s as an expandable array of chars. (Double its size whenever we run out of space.) Cast the final array to a String.

Ans: Store s as an expandable array of chars. (Double its size whenever we run out of space.) Cast the final array to a String. CMSC 131: Chapter 21 (Supplement) Miscellany Miscellany Today we discuss a number of unrelated but useful topics that have just not fit into earlier lectures. These include: StringBuffer: A handy class

More information

CS112 Lecture: Characters and Strings

CS112 Lecture: Characters and Strings CS112 Lecture: Characters and Strings Objectives: Last Revised 3/21/06 1. To introduce the data type char and related basic information (escape sequences, Unicode). 2. To introduce the library classes

More information

CSC 222: Object-Oriented Programming. Spring 2017

CSC 222: Object-Oriented Programming. Spring 2017 CSC 222: Object-Oriented Programming Spring 2017 Simulations & library classes HW3: RouletteWheel, RouletteGame, RouletteTester javadoc java.lang classes: String, Character, Integer java.util.random for

More information

Index COPYRIGHTED MATERIAL

Index COPYRIGHTED MATERIAL Index COPYRIGHTED MATERIAL Note to the Reader: Throughout this index boldfaced page numbers indicate primary discussions of a topic. Italicized page numbers indicate illustrations. A abstract classes

More information

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 ); Sample Final Exam 1. Evaluate each of the following expressions and show the result and data type of each: Expression Value Data Type 14 % 5 1 / 2 + 1 / 3 + 1 / 4 4.0 / 2.0 Math.pow(2.0, 3.0) (double)(2

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Version of January 21, 2013 Abstract Review of object-oriented programming concepts: Implementing

More information

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness. Methods There s a method in my madness. Sect. 3.3, 8.2 1 Example Class: Car How Cars are Described Make Model Year Color Owner Location Mileage Actions that can be applied to cars Create a new car Transfer

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 26 March 23, 2016 Inheritance and Dynamic Dispatch Chapter 24 Inheritance Example public class { private int x; public () { x = 0; } public void incby(int

More information

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID: CMPS 12A - Winter 2002 Final Exam A March 16, 2002 Name: ID: This is a closed note, closed book exam. Any place where you are asked to write code, you must declare all variables that you use. However,

More information

COP 3330 Final Exam Review

COP 3330 Final Exam Review COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time

More information

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each Your Name: Your Pitt (mail NOT peoplesoft) ID: Part Question/s Points available Rubric Your Score A 1-6 6 1 pt each B 7-12 6 1 pt each C 13-16 4 1 pt each D 17-19 5 1 or 2 pts each E 20-23 5 1 or 2 pts

More information

CSC 222: Object-Oriented Programming. Spring 2013

CSC 222: Object-Oriented Programming. Spring 2013 CSC 222: Object-Oriented Programming Spring 2013 Simulations & library classes HW3: RouletteWheel, RouletteGame, RouletteTester javadoc java.lang classes: String, Character, Integer java.util.random for

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016 First Name (Print): Last Name (Print): Student Number: The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II 2016 Instructor: Dr. Bowen Hui Tuesday, April 19, 2016 Time: 6:00pm

More information

Brief Summary of Java

Brief Summary of Java Brief Summary of Java Java programs are compiled into an intermediate format, known as bytecode, and then run through an interpreter that executes in a Java Virtual Machine (JVM). The basic syntax of Java

More information

Advanced Object Concepts

Advanced Object Concepts Understanding Blocks Blocks - Appears within any class or method, the code between a pair of curly braces Outside block- The first block, begins immediately after the method declaration and ends at the

More information

Object Oriented Programming in C#

Object Oriented Programming in C# Introduction to Object Oriented Programming in C# Class and Object 1 You will be able to: Objectives 1. Write a simple class definition in C#. 2. Control access to the methods and data in a class. 3. Create

More information

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods

More information

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2009 Test 2 Question Max Mark Internal

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. Data structures Collections of related data items. Discussed in depth in Chapters 16 21. Array objects Data

More information

Chief Reader Report on Student Responses:

Chief Reader Report on Student Responses: Chief Reader Report on Student Responses: 2017 AP Computer Science A Free-Response Questions Number of Students Scored 60,519 Number of Readers 308 Score Distribution Exam Score N %At Global Mean 3.15

More information

Introduction to Classes and Objects. David Greenstein Monta Vista High School

Introduction to Classes and Objects. David Greenstein Monta Vista High School Introduction to Classes and Objects David Greenstein Monta Vista High School Client Class A client class is one that constructs and uses objects of another class. B is a client of A public class A private

More information

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

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Built in Libraries and objects CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Objects and Built in Libraries 1 Classes and Objects An object is an

More information

Topic 3 Encapsulation - Implementing Classes

Topic 3 Encapsulation - Implementing Classes Topic 3 Encapsulation - Implementing Classes And so, from Europe, we get things such as... object-oriented analysis and design (a clever way of breaking up software programming instructions and data into

More information

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 10 Exercise

More information

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com CHAPTER 8 OBJECTS AND CLASSES Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/2011 Chapter Goals To understand the concepts of classes, objects and encapsulation To implement instance variables,

More information

Strings! Today! CSE String Methods!

Strings! Today! CSE String Methods! Strings CSE 1710 Lecture 14, 15 String Handling We have covered three chunks of material: Week 1: String Literals pp. 22-23; Fig 1.12; PT 1.8 Week 6: The String Class Section 6.1.1, pp. 219-220 The Masquerade

More information

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition Chapter 4: Writing Classes Objects An object has: Presentation slides for state - descriptive characteristics Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus,

More information

Java Strings Java, winter semester

Java Strings Java, winter semester Java Strings 1 String instances of java.lang.string compiler works with them almost with primitive types String constants = instances of the String class immutable!!! for changes clases StringBuffer, StringBuilder

More information

Chapter 9. Objects and Classes

Chapter 9. Objects and Classes Chapter 9 Objects and Classes 1 OO Programming in Java Other than primitive data types (byte, short, int, long, float, double, char, boolean), everything else in Java is of type object. Objects we already

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1: Introduction Lecture Contents 2 Course info Why programming?? Why Java?? Write once, run anywhere!! Java basics Input/output Variables

More information

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1. Overview Inf1-OOP Creating Data Types 1 Circle Class Object Default Perdita Stevens, adapting earlier version by Ewan Klein Format Strings School of Informatics January 11, 2015 HotelRoom Class More on

More information

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal APCS A Midterm Review You will have a copy of the one page Java Quick Reference sheet. This is the same reference that will be available to you when you take the AP Computer Science exam. 1. n bits can

More information

- Thus there is a String class (a large class)

- Thus there is a String class (a large class) Strings - Strings in Java are objects - Thus there is a String class (a large class) - In a statement like this: System.out.println( Hello World ); the Java compiler creates a String object from the quoted

More information

CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture. CSE1710 Click to edit Master Week text 12, styles Lecture 22 Second level Third level Fourth level Fifth level Fall 2014 Tuesday, Nov 25, 2014 1 Big Picture Tuesday, Dec 02 is designated as a study day.

More information

Documenting, Using, and Testing Utility Classes

Documenting, Using, and Testing Utility Classes Documenting, Using, and Testing Utility Classes Readings: Chapter 2 of the Course Notes EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Structure of Project: Packages and Classes

More information

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007 Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007 Java We will be programming in Java in this course. Partly because it is a reasonable language, and partly because you

More information

OOP-Lecture Java Loop Controls: 1 Lecturer: Hawraa Sh. You can use one of the following three loops: while Loop do...while Loop for Loop

OOP-Lecture Java Loop Controls: 1 Lecturer: Hawraa Sh. You can use one of the following three loops: while Loop do...while Loop for Loop Java Loop Controls: You can use one of the following three loops: while Loop do...while Loop for Loop 1- The while Loop: A while loop is a control structure that allows you to repeat a task a certain number

More information

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Things to Review Review the Class Slides: Key Things to Take Away Do you understand

More information

Chapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods

Chapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods Chapter 3 Classes Lesson page 3-1. Classes Activity 3-1-1 The class as a file drawer of methods Question 1. The form of a class definition is: public class {

More information

COE318 Lecture Notes Week 4 (Sept 26, 2011)

COE318 Lecture Notes Week 4 (Sept 26, 2011) COE318 Software Systems Lecture Notes: Week 4 1 of 11 COE318 Lecture Notes Week 4 (Sept 26, 2011) Topics Announcements Data types (cont.) Pass by value Arrays The + operator Strings Stack and Heap details

More information

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Handout 7. Defining Classes part 1. Instance variables and instance methods. Handout 7 CS180 Programming Fundamentals Spring 15 Page 1 of 8 Handout 7 Defining Classes part 1. Instance variables and instance methods. In Object Oriented programming, applications are comprised from

More information

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 6 problems on the following 6 pages. You may use your two-sided hand-written 8 ½ x 11 note sheet during the exam.

More information

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Object-oriented programming מונחה עצמים) (תכנות involves programming using objects An object ) represents (עצם an entity in the real world that can be distinctly identified

More information

EECS168 Exam 3 Review

EECS168 Exam 3 Review EECS168 Exam 3 Review Exam 3 Time: 2pm-2:50pm Monday Nov 5 Closed book, closed notes. Calculators or other electronic devices are not permitted or required. If you are unable to attend an exam for any

More information

CHAPTER 6 MOST COMMONLY USED LIBRARIES

CHAPTER 6 MOST COMMONLY USED LIBRARIES LIBRARY CHAPTER 6 - A set of ready-made software routines (class definitions) that can be reused in new programs, is called a Library. - Some commonly used java libraries are : Math Library String Library

More information

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics Strings Strings and their methods Produced by: Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list Primitive Types: char Object Types: String Primitive vs Object Types

More information

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 6 problems on the following 6 pages. You may use your two-sided hand-written 8 ½ x 11 note sheet during the exam.

More information