CC438: Exercises I Write down the output and explain - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

CC438: Exercises I Write down the output and explain

Description:

System.out.println(a.equals(x)); Sample Question (contd) static class A { int x; ... Write an equals method for class A such that: new A(x).equals(new Integer(x) ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 7
Provided by: simonm3
Category:

less

Transcript and Presenter's Notes

Title: CC438: Exercises I Write down the output and explain


1
CC438 Exercises IWrite down the output and
explain
  • public class SwapPrim
  • public static void main(String args)
  • int x 10
  • int y 20
  • swap(x, y)
  • System.out.println(x " " y)
  • public static void swap(int x, int y)
  • int tmp x
  • x y
  • y tmp

2
Write down the output and explain
  • public class SwapArray
  • public static void main(String args)
  • int x new int10, 20
  • swap(x)
  • System.out.println(x0 " " x1)
  • public static void swap(int x)
  • int tmp x0
  • x0 x1
  • x1 tmp

3
Write down the output and explain
  • public class Distance
  • public static void main(String args)
  • // what output do you expect?
  • System.out.println(distance(0, 0, 3, 4))
  • public static double distance( double x1,
    double y1, double x2, double y2)
  • return Math.sqrt((x1 - x2) (x1 - x2)
    (y1 - y2) (y1 - y2))
  • Extend this example to work in three dimension
    instead of two
  • Think about the possible ways of doing this
    before writing any code
  • Hint for a good solution, the method signature
    might need to change

4
Syntax Check
  • Which of the following (independent lines) is
    legal?
  • int x null
  • Integer x null
  • int x null
  • double x 1
  • int x 1.0
  • Integer x 1 int y x

5
Sample Exam Question (20)
  • Explain the difference between the operator
    and the method equals() in Java. Hence explain
    what the following code would output when the
    main method is run
  • public class TestEquals
  • public static void main(String args)
  • Integer x new Integer(10)
  • Integer y new Integer(10)
  • System.out.println(x y)
  • System.out.println(x.equals(y))
  • A a new A(10)
  • A b new A(10)
  • System.out.println(a.equals(a))
  • System.out.println(a.equals(b))
  • System.out.println(a b)
  • System.out.println(a.equals(x))

6
Sample Question (contd)
  • static class A
  • int x
  • public A(int x) this.x x
  • public String toString()
  • return "" x
  • Write an equals method for class A such that
  • new A(x).equals(new Integer(x))
  • would return true for any int value of x.
Write a Comment
User Comments (0)
About PowerShow.com