Download presentation
Presentation is loading. Please wait.
Published byGillian Allison Morton Modified over 6 years ago
1
Java Graphics Here is a specification for method drawOval, which appears in class Graphics. // Draw an oval that is bounded by the rectangle with upper-left // corner (x,y), width w, and height h public void drawOval(int x, int y, int w, int h) Fill in the body of the following method. Declare any additional (local) variables that you need. // Draw a circle, using Graphics g, with center (x,y) and radius r public void drawCircle(Graphics g, int x, int y, int r) { g.drawOval( ____________, ____________, ____________, ____________); }
2
Arithmetic expressions
Fill in the body for method computeAverage. It should do exactly what is described in the comments that precede it. Declare any local variables that you need. // Compute the floating point average of three integers x, y, and z and print // the result to the output window. // In addition, if the average is above 10, print an “Average is above 10” message // to the output window. public void computeAverage (int x, int y, int z) { //your code goes in here }
3
Scoping scope of a name is the area of program within which it can be referenced a variable must be declared before being used can only be declared once within a method scope of a local variable in a method is the sequence of statements that follow it in the method and that are in the same block ({….}) scope of a formal parameter is the sequence of statements in the method body
4
Scoping scope of a public field is the bodies of all methods in the class (and the declarations of fields that follow) and anywhere the class can be referenced (e.g., usually the main method) scope of a private field is the bodies of all methods in the class (and the declarations of fields that follow) cannot redeclare a formal parameter within the method if you declare a local variable of the same name as a field, then you can’t reference that field in that method
5
Scoping In each class below, do the following. (1) Circle each variable reference that is illegal. (2) For each legal reference, draw an arrow from it to the declaration of the local variable, parameter, or field to which it refers. Don’t circle or draw arrows from the declarations. public class Weird { public int w; public int x; public void Method1 (int y, int w) { w = 17; v = y; } public void Method2 () { int y; int z = v; y = 6; x = w;
6
Classes Consider the ATM class below. It defines the specifications for an Automated Teller Machine object for one person’s ATM account. // An Automated Teller Machine class. public class Atm { public int balance; // balance remaining in the account in whole dollars // Prints the balance to the output window. public void printBalance ( ) { System.out.println (“Balance is: ’’ + balance); } // Adds amount_to_deposit to the balance. public void deposit (int amount_to_deposit ){ balance = balance + amount_to_deposit;
7
Classes (cont.) Fill out the body of the main method below so that it performs the following steps in order: (1) create an instance of an Atm object called my_atm; (2) initialize the balance to $1000 and print it to the output window using the printBalance method; (3) deposit $50 to the account using method deposit and print the new balance to the output window, again using the printBalance method. For full credit, your code should include proper comments and indentation. public class Question { public static void main ( String args [ ] ) { //your code goes in here }
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.