Top Banner
ast lecture nd objects variables (fields) methods s – formal parameter, actual parameter pe, void iables (fields) static hods static fi ivate, accessor,mutator methods Name(); or, constructor overloading verloading
23

Last lecture

Feb 06, 2016

Download

Documents

kalb

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 - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Last lecture

Last lectureclasses and objectsinstance variables (fields)instance methodsparameters – formal parameter, actual parametersreturn type, voidclass variables (fields) staticclass methods static constants final staticpublic/private, accessor,mutator methodsnew ClassName(); Constructor, constructor overloadingmethods overloading

Page 2: Last lecture

Last lecturevariables of class/primitive type (references)null

Page 3: Last lecture

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?

Page 4: Last lecture

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?

Page 5: Last lecture

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?

Page 6: Last lecture

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?

Page 7: Last lecture

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?

Page 8: Last lecture

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?

Page 9: Last lecture

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?

Page 10: Last lecture

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);

Page 11: Last lecture

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(); }}

Page 12: Last lecture

Guess a numberI think an integer from {1,2,...,1000}

You have 10 guesses. After each guess I tell you whether the numberyou guessed is too small or too large.Can you always win?

Page 13: Last lecture

Guess a numberI think an integer from {1,2,...,1000}

You have 10 guesses. After each guess I tell you whether the numberyou guessed is too small or too large.Can you always win?

How many guesses would you need if the number is from {1,2,....,1000000}?

Page 14: Last lecture

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 amongnumbers {1,...,n}its only divisors are 1 and n

You can check whether a is divisorof n by testing(n%a==0)

Page 15: Last lecture

EXERCISE #11:

Write a piece of code which wouldcount the number of primes in{1,....,1000}.

Page 16: Last lecture

Class String String a,b;

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

Page 17: Last lecture

Class String

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

Page 18: Last lecture

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 firstoccurence of b removed

rFO(“Hello World!”,”World”) -> “Hello !”rFO(“Hello!”,”World”) -> “Hello!”

Page 19: Last lecture

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”

Page 20: Last lecture

Class StringBufferString 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);}

Page 21: Last lecture

Class StringBufferString 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);}

?

Page 22: Last lecture

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?

Page 23: Last lecture

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?