CPSC 233 Tutorial Xin Mar 2, 2011. toString() method Add a toString() to a class to enable the print function public String toStinrg ( )

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
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.
Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
James Tam Advanced Java Programming After mastering the basics of Java you will now learn more complex but important programming concepts as implemented.
James Tam CPSC 233: Introduction to Classes and Objects, Part II More on Java methods Relations between classes Association Aggregation Multiplicity Issues.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
James Tam CPSC 233: Advanced Java And Object-Oriented Concepts More on Java methods Relations between classes Association Aggregation Multiplicity Issues.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Introduction to Java Programming Language May 2015 Kyung Eun Park COSC Introduction to Computer Science II.
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
Topic 11 Scanner object, conditional execution Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
James Tam Advanced Java Programming After mastering the basics of Java you will now learn more complex but important programming concepts as implemented.
Advanced Java Programming CS 537 – Data Structures and Algorithms.
1 BUILDING JAVA PROGRAMS CHAPTER 6 DETAILS OF TOKEN-BASED PROCESSING.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
CPSC 233 Tutorial Xin Jan 24, Assignment 1 Due on Jan 28 at 4:00 PM Part I  Assignment Box on 2 nd floor Part II  Submitted electronically on.
Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.
Announcements Final Exam:TBD. public static void main(String [] args) { final int ASIZE = 5; int [] intArray= new int[ASIZE]; for(int i = 0; i < ASIZE;
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number.
TIC TAC TOE. import java.util.Scanner; import java.util.Random; public class PlayTTT{ public static void main(String[]args){ Scanner reader = new Scanner(System.in);
Objects & Classes Weiss ch. 3. So far: –Point (see java.awt.Point) –String –Arrays of various kinds –IPAddress (see java.net.InetAddress) The Java API.
Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Sit-In Lab 2 - OOP Restaurant.  Manage a restaurant and perform these types of queries: Assign a favorite table to a specific group Assign the lexicographically-smallest.
Library Books exercise cosc The classes Library class +ArrayList -ArrayList + Library() +initializeDefaultCategories():void +displayAllCategories():void.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Take-Home Lab #02 CS1020 – DATA STRUCTURES AND ALGORITHMS 1 AY SEMESTER 2 1.
Programming Fundamentals 2: Libraries/ F II Objectives – –utilize some useful Java libraries e.g. String, Scanner, HashMap, and Random.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Winter 2006CISC121 - Prof. McLeod1 Stuff We had better discuss a midterm date… –27 Feb. to 3 March or –6 to 10 March.
CPSC 233 Tutorial Xin Liu Feb 14, Tips on keyboard input How to detect that the user just hit enter when prompted for a string import java.util.Scanner;
Chapter 9 Introduction to Arrays Fundamentals of Java.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
OOP (Java): Libraries/ OOP (Java) Objectives – –utilize some useful Java libraries e.g. String, Scanner, HashMap, and Random Semester.
Three kinds of looping structures The while loop The for loop The do (also called the do-while) loop All have equivalent power, e.g., if you can write.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
CSC111 Quick Revision.
Chapter 7 User-Defined Methods.
Computer Programming Methodology Input and While Loop
Topic 11 Scanner object, conditional execution
Exercise on Java Basics
Introduction to Classes and Methods
COMPUTER 2430 Object Oriented Programming and Data Structures I
Introduction to Computing Using Java
class PrintOnetoTen { public static void main(String args[]) {
Advanced Java Programming
F II 6. Using Libraries Objectives
Advanced Java Programming
MIS 222 – Lecture 12 10/9/2003.
CPSC 233 Tutorial 13 March 11/12th, 2015.
Presentation transcript:

CPSC 233 Tutorial Xin Mar 2, 2011

toString() method Add a toString() to a class to enable the print function public String toStinrg ( )

Example public class Foo { private int num; public Foo () { num = 0; } public Foo (int n) { setNum(n); } public int getNum () { return num; } public void setNum (int n) { num = n; } public String toString () { String temp = "Num="; temp = temp + num; return temp; } public boolean equals (Foo f) { return(this.num == f.num); } public class ReferenceDriver { public static void main (String [] args) { Foo f1; f1 = new Foo(10); System.out.println(f1); }

Deep copy vs. Shallow copy Deep copy Create a new object, and make it equal to the old one Shadow copy Just return the reference

Example public class MyCopy { public Foo shallowCopy (Foo aFoo) { Foo temp = aFoo; return temp; } public Foo deepCopy (Foo aFoo) { Foo temp = new Foo (); temp.setNum(aFoo.getNum()); return temp; } public class ReferenceDriver { public static void main (String [] args) { Foo f1; Foo f2; MyCopy aCopier = new MyCopy(); f1 = new Foo(10); f2 = aCopier.deepCopy(f1); f2.setNum(20); System.out.println(f1); System.out.println(f2); }

Static variables All objects share the same copy Can be used as “global” variables Use directly with class name eg. Mode.debug Even no objects are created

Static method Use with the class name eg. foo.max() don’t need to create an object although you can still use them with object name Static method access static attributes ✓ access non-static attributes ✗ attributes belong to objects call another static method ✓ call non-static class method ✗ using this reference ✗

Example class staticExample { public static int number = 1; public static int max (int n1, int n2) { if (n1 > n2) return n1; else return n2; } class staticExampleDriver { public static void main (String [] args) { int result = staticExample.max(1, 2); System.out.println("the result is " + result); result = staticExample.number; System.out.println("the result is " + result); staticExample eg = new staticExample(); result = eg.max(2, 3); System.out.println("the result is " + result); }

Read from a file import libraries import java.util.Scanner; import java.io.*; Open a file Scanner inputStream = new Scanner(new FileInputStream(“filename.txt”)); Read the file String line = inputStream.nextLine (); Close the file inputStream.close ();

Improve the grid program Initialize array from a txt file add tokens to random vacant space