1 / 23

Last lecture

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

kalb
Download Presentation

Last lecture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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: 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(); } } 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); } } What is the output?

  4. EXERCISE #2: What is the output? 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));

  5. EXERCISE #3: What is the output? 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));

  6. EXERCISE #4: What is the output? 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));

  7. EXERCISE #5: 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);

  8. EXERCISE #6: 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); What is the output?

  9. EXERCISE #7: 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); 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. You can check whether a is divisor of n by testing (n%a==0) 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

  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 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!” String substring(int start,int end); int indexOf(String aString); int length()

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

  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 What is the output? 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); } }

  23. EXERCISE #15: What is the output? 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); } }

More Related