{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw1sol

# hw1sol - CMPSC 20 Solution of HW1 Note In grading only...

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

Solution of HW1 Note: In grading: only problems 3,5,8,9 were graded out of 10,20,10,10 respectively. The total is out of 50 and the average is 38.6 ---------------------------------------------------------------- 1- The output must be 0 regardless of the body of the method f(int x). This is because x is passed to the method by value rather than by reference. ---------------------------------------- 2- public class Counter { // the counting field private int c; // constructor initializes counter to zero public Counter() { c=0; } // setting the counter public void setCounter(int x) { if (x>=0) c=x; } // increment counter public void incCounter() { c++; } // decrement counter public void decCounter() { if (c>0) c--; } // accessor method public int getCounter() { return c; } // display on screen public void display() { System.out.println("Counter value is: " + c); } // A program to test the class public static void main(String args[]) { Counter cnt=new Counter(); cnt.display(); cnt.incCounter();

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 4

hw1sol - CMPSC 20 Solution of HW1 Note In grading only...

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

View Full Document
Ask a homework question - tutors are online