Last lecture classes and objects instance variables (fields) instance methods parameters – formal parameter, actual parameters return type, void class.

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

Computer Science A 9: 3/11. Inheritance Today: Inheritance (JC – CCJ ) I have to leave at 11am (but you can stay)
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown,
Enhancing classes Visibility modifiers and encapsulation revisited
Arrays grades for students in a large course we need a lot of variables double[] grade; grade=new double[numberOfStudents]; creates “variables” grade[0],grade[1],...,grade[numberOfStudents-1]
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP Introduction to Programming Adrian Ilie July 13, 2005.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
28-Jun-15 String and StringBuilder Part I: String.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
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 Program = collection of Classes Class::Object abstract concept instances of the class BankAccount::BankAccountOfJohnSmith#127.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Programming with methods and classes. 2 Methods  Instance (or member) method Operates on a object (i.e., and instance of the class) String s = new.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Methods We write methods in our programs for many reasons:
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Programming with methods and classes. Methods  Instance method Operates on a object (i.e., and instance of the class) String s = new String("Help every.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
17-Feb-16 String and StringBuilder Part I: String.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Constructor OverloadingtMyn1 Constructor Overloading One context in which you will regularly need to use overloading is when you write constructors for.
Classes - Intermediate
Utilities (Part 2) Implementing static features 1.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
Chapter 7: Programming with methods and classes..
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
Recursion occurs when a method calls itself. public class RecursionOne { public void run(int x) { System.out.println(x); run(x+1); } public static void.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Java: Base Types All information has a type or class designation
Defining Your Own Classes II
Functions + Overloading + Scope
Topic: Classes and Objects
GC211 Data structure Lecture 3 Sara Alhajjam.
Java: Base Types All information has a type or class designation
Some Eclipse shortcuts
Lecture 17: Polymorphism (Part II)
CSC240 Computer Science III
Class Structure 16-Nov-18.
Programming with methods and classes
March 29th Odds & Ends CS 239.
Introduction to Java Programming
An Introduction to Java – Part I, language basics
Class Structure 28-Nov-18.
Class Structure 7-Dec-18.
Assignment 7 User Defined Classes Part 2
Class Structure 2-Jan-19.
class PrintOnetoTen { public static void main(String args[]) {
Namespaces, Scopes, Access privileges
Class Structure 25-Feb-19.
Lecture 18: Polymorphism (Part II)
Recursion Method calling itself (circular definition)
Lecture 6: References and linked nodes reading: 16.1
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Last lecture classes and objects instance variables (fields) instance methods parameters – formal parameter, actual parameters return type, void class variables (fields) static class methods static constants final static public/private, accessor,mutator methods new ClassName(); Constructor, constructor overloading methods overloading

Last lecture variables of class/primitive type (references) null

EXERCISE #1: Class Test { private int x,y; public Test(int a,int b) { x=a; y=b; System.out.println (“”+a+”, ”+b); } public Values() { System.out.println (“”+x+”,”+y); } public class Runs { public main(String[] args) { Test r,p; r=new Test(2,3); r.Values(); p=r; p=new Test(3,4); r.Values(); } What is the output?

EXERCISE #2: public class MyClass { private int data; public boolean equals(MyClass a) { return (this==a) } public MyClass(int x) { data=x; } } MyClass a,b; a=new MyClass(3); b=a; System.out.println(a.equals(b)); b=new MyClass(3); System.out.println(a.equals(b)); What is the output?

EXERCISE #3: public class MyClass { private int data; public boolean equals(MyClass a) { return (this.data==a.data) } public MyClass(int x) { data=x; } } MyClass a,b; a=new MyClass(3); b=a; System.out.println(a.equals(b)); b=new MyClass(4); System.out.println(a.equals(b)); What is the output?

EXERCISE #4: public class MyClass { private String name; public boolean equals(MyClass a) { return (this.name==a.name) } public MyClass(String name) { this.name=name; } } MyClass a,b; a=new MyClass(“John”); b=a; System.out.println(a.equals(b)); b=new MyClass(“John”); System.out.println(a.equals(b)); What is the output?

public class myInt { public int x; } myInt a = new myInt(); a.x = 10; myInt b = a; System.out.println("a = "+a.x+"; b = "+b.x); b.x = 20; System.out.println("a = "+a.x+"; b = "+b.x); EXERCISE #5: What is the output?

public class myInt { public int x; public static void switch(myInt d,myInt e) { int f; f=d.x; d.x=e.x; e.x=f; } public static void switch(int d,int e) { int f; f=d; d=e; e=f; } myInt a = new myInt(); a.x = 10; myInt b = new myInt(); b.x = 20; myInt.switch(a.x,b.x); System.out.println("a = "+a.x+"; b = "+b.x); myInt.switch(a.b); System.out.println("a = "+a.x+"; b = "+b.x); EXERCISE #6: What is the output?

public class myString{ public String x; public static void switch(myInt d,myInt e) { String f; f=d.x; d.x=e.x; e.x=f; } public static void switch(String a,String b) { String f; f=d; d=e; e=f; } myString a = new myString(); a.x = “10”; myString b = new myString(); b.x = “20”; myString.switch(a.x,b.x); System.out.println("a = "+a.x+"; b = "+b.x); myString.switch(a.b); System.out.println("a = "+a.x+"; b = "+b.x); EXERCISE #7: What is the output?

EXERCISE #8: What is the output? class Test { public int x; public static int y; public Test(int x,int y) { this.x=x; this.y=y; } } Test a,b; a=new Test(3,4); System.out.println(""+a.x+","+a.y); b=new Test(5,6); System.out.println(""+b.x+","+b.y); System.out.println(""+a.x+","+a.y);

EXERCISE #9: What is the output? public class Test { static Test first=null; Test next; public int x; public Test(int x) { this.x=x; next=first; first=this; } public static void list() { for (Test p=first;p!=null;p=p.next) System.out.println(" "+p.x); } public static void main(String[] args) { Test a=new Test(2),b=new Test(3),c=new Test(4); Test.list(); }

Guess a number I think an integer from {1,2,...,1000} You have 10 guesses. After each guess I tell you whether the number you guessed is too small or too large. Can you always win?

Guess a number I think an integer from {1,2,...,1000} You have 10 guesses. After each guess I tell you whether the number you guessed is too small or too large. Can you always win? How many guesses would you need if the number is from {1,2,...., }?

EXERCISE #10: import javax.swing.*; public class Sum { public static void main(String args[]) { int number; boolean numberIsAPrime; number=Integer.parseInt(JOptionPane.showInputDialog("Enter the number:")); /* */ if (numberIsAPrime) JOptionPane.showMessageDialog(null,“is a prime"); else JOptionPane.showMessageDialog(null,“is not prime"); System.exit(0); } Number n is a prime if among numbers {1,...,n} its only divisors are 1 and n You can check whether a is divisor of n by testing (n%a==0)

EXERCISE #11: Write a piece of code which would count the number of primes in {1,....,1000}.

Class String String a,b; a=“hello”; b=a+42; a=b+”hello” System.out.println(“”+a.length());

Class String Other methods: boolean equals(String otherString); boolean equalsIgnoreCase(String o); String substring(int start,int end); int indexOf(String aString);

EXERCISE #12 String substring(int start,int end); int indexOf(String aString); int length() Define a method String removeFirstOccurrence(String a,String b) which returns String a with the first occurence of b removed rFO(“Hello World!”,”World”) -> “Hello !” rFO(“Hello!”,”World”) -> “Hello!”

EXERCISE #13 String substring(int start,int end); int length() Define a method String reverse(String a); which returns the String a reversed rFO(“Hello World!”) -> “!dlroW olleH” rFO(“Hello!”) -> “!olleH”

Class StringBuffer String reverse(String a) { StringBuffer b=new StringBuffer(a); int n=a.length; for (i=0;i<n/2;i++) { char c; c=b.charAt(i); b.setCharAt(i,b.charAt(n-i-1)); b.setCharAt(n-i-1,c); } return toString(b); }

Class StringBuffer String reverse(String a) { StringBuffer b=new StringBuffer(a); int n=a.length; for (i=0;i<n;i++) { char c; c=b.charAt(i); b.setCharAt(i,b.charAt(n-i-1)); b.setCharAt(n-i-1,c); } return toString(b); } ?

EXERCISE #14 public class Test { public static void main(String[] args) { int x=1,y=2; { int x=3; System.out.println(“”+x+”,”+y); x=y; } System.out.println(“”+x+”,”+y); } What is the output?

EXERCISE #15: public class Runs { public void main(String args) { int x=14,y=7,z=5,i; for (i=0;i<300;i++) { x = x+y+z; y = x-y-z; z = x-y-z; x = x-y-z; } System.out(“”+x+”,”+y+”,”+z); } What is the output?