Chapter-10

Chapter-10 - Exercise 1 public class Circle cfw private...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 1 Exercise 1 public class Circle { private double radius; public Circle() { radius = 0.1; } public void setRadius(double r) { radius = r ;} public double getRadius() {return radius;} }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 2 public class CircleTest { public static void main(String[] args) { double d = 0.08; Circle c = new Circle(); System.out.println( d ); System.out.println( c.getRadius() ); foo( d, c ); System.out.println( d ); System.out.println( c.getRadius() ); } public static void foo( double f, Circle circle) { f = 0.5; circle.setRadius(6.0); } } Output ??
Background image of page 2
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 3 Exercise 2 String s1 = new String(“Toronto”); String s2 = new String(“Ottawa”); int j = s1.compareTo(s2); int k = s2.compareTo(s1); is j +ve, -ve or 0 ?? is k +ve, -ve or 0 ??
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 4 Exercise 3 String s1 = new String(“Toronto”); System.out.println(s1.substring(0, 3).toUpperCase()); Output ???
Background image of page 4
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 5 Exercise 4 int sum( int x, int y ) { int result; result = x + y; } Find error and explain how to correct it ???
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 6 Static variables and Static Methods
Background image of page 6
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 7 Static class members a static method is invoked through its class name double d = Math.sqrt(49); variables can be static as well static methods - class methods static variables – class variables
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 8 Static class members declared using static modifier private static int count; public static int getCount() {…}
Background image of page 8
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 9 Static variables public class Circle { private double radius; private static int count; public Circle() { radius = 1.0; count++; } ………… }
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Static methods static methods never use instance variable values.
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/12/2010 for the course COE 318 taught by Professor Ken during the Spring '08 term at Ryerson.

Page1 / 32

Chapter-10 - Exercise 1 public class Circle cfw private...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online