hw1sol

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

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

View Full Document Right Arrow Icon
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();
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online