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
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
Last lecturevariables 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 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?
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}?
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)
EXERCISE #11:
Write a piece of code which wouldcount 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 firstoccurence 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 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);}
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);}
?
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?