Chapter-10

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

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

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;} }

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

View Full Document
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 ??
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 ??

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

View Full Document
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 ???
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 ???

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

View Full Document
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 6 Static variables and Static Methods
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

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

View Full Document
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() {…}
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++; } ………… }

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

View Full Document
10 Static methods static methods never use instance variable values.
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
Ask a homework question - tutors are online