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...
}
import csbsju.cs160.*;
public class PrintTriangle {
public static void printlnStars(int num) {
for(int i = 0; i < num; i++) IO.print("*");
IO.println();
}
public static void main(String[] args) {
IO.print("Width? ");
int n = IO.readInt();
for(int i = n; i > 0; i--) printlnStars(i);
}
}
r 12 8 4 0 m 32 20 12 8 4 n 20 12 8 4 0
public class Rect {
private double width;
private double height;
public Rect(double w, double h) {
width = w;
height = h;
}
public double getPerimeter() {
return 2 * width + 2 * height;
}
public double getArea() {
return width * height;
}
public boolean canContain(Rect other) {
return (width >= other.width && height >= other.height)
|| (width >= other.height && height >= other.width);
}
}