One-page version suitable for printing.
Write a program to print a triangle of asterisks as in the third problem of Lab 0.
Width? 5 ***** **** *** ** *
import csbsju.cs160.*;
public class PrintTriangle {
public static void printlnStars(int num) {
}
public static void main(String[] args) {
}
}
Consider the following method to compute the greatest common denominator of two numbers.
public static int gcd(int m, int n) {
int r;
while(n != 0) {
r = m % n;
m = n;
n = r;
}
return m;
}
r m 32 n 20
The following program uses the Account class we defined in class.
import csbsju.cs160.*;
public class PrintHello {
public static void main(String[] args) {
Account a = new Account();
Account b = new Account();
a.deposit(81.0);
b.withdraw(3.0);
a = b;
a.deposit(9.0);
b = new Account();
b.withdraw(1.0);
IO.println(a.getBalance());
}
}
Consider the following class method.
public static void mystery(int[] arr) {
for(int i = arr.length - 1; i > 0; i--) {
arr[i] = 10 + arr[i - 1];
}
for(int i = 0; i < arr.length; i++) {
IO.print(arr[i]);
}
}
Define a new class Rect that can be used to create an object representing a rectangle. The constructor method for Rect should take two parameters, both doubles: the rectangle's width and the rectangle's height. The class should support the following object methods.
| double getPerimeter() | Returns the length of the rectangle's perimeter. |
| double getArea() | Returns the area of the rectangle. |
| boolean canContain(Rect other) | Returns true if other can fit within the rectangle. (in its current configuration or rotated 90 degrees). |
public class Rect {
// your answer here...
}