Download presentation
Presentation is loading. Please wait.
Published byChristina Gallagher Modified over 9 years ago
1
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
2
Last lecture variables of class/primitive type (references) null
3
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?
4
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?
5
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?
6
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?
7
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?
8
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?
9
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?
10
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);
11
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(); }
12
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?
13
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,....,1000000}?
14
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)
15
EXERCISE #11: Write a piece of code which would count the number of primes in {1,....,1000}.
16
Class String String a,b; a=“hello”; b=a+42; a=b+”hello” System.out.println(“”+a.length());
17
Class String Other methods: boolean equals(String otherString); boolean equalsIgnoreCase(String o); String substring(int start,int end); int indexOf(String aString);
18
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!”
19
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”
20
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); }
21
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); } ?
22
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?
23
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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.