/******************************************* Solutions of Review #2 - Question 1. *******************************************/ k= 0 k= 1 k= 2 k= 3 j= 14 /******************************************* Solutions of Review #3 - Question 2. *******************************************/ /* Solution using for loop */ int row, col; System.out.print("Enter integer n: number of lines "); int n = JLiveRead.readInt(); for (row =1; row<=n; row++) { for (col=1; col<=row; col++) { if ( row %2 == 1 ) System.out.print("*"); else System.out.print("?"); } System.out.println(); } /* Alternate solution using while loop */ int n = JLiveRead.readInt(); int row = 1; while ( row <= n ) { int col = 1; while( col <= row ) { if ( row %2 == 1 ) System.out.print("*"); else System.out.print("?"); col++; } System.out.println(); row++; } /******************************************* Solutions of Review #3 - Question 3. *******************************************/ /** An instance of Student has name and three scores */ public class Student{ private String name; //Student name private int score1; //Homework score 1 private int score2; //Homework score 2 private int score3; //Homework score 3 //The constants below are made public so that another class can see //them, as requested by the question statement. public static final int PASS=0; //average is in 70..100 public static final int MARGINAL=1; //average is in 55..69 public static final int FAILING=2; //average is in 0.. 54 /** Constructor to initialize all fields */ public Student(String s, int sc1, int sc2, int sc3) //Alternately, one can specify setter methods to assign to the fields /** Getter methods for the fields */ public String getName() public int getScore1() public int getScore2() public int getScore3() /** = Compute average of the three scores */ public int average() //return type double is also ok /** = Student status: PASS, MARGINAL, or FAILING */ public int getStatus() } /******************************************* Solutions of Review #3 - Question 4. *******************************************/ public class Calculation { public static void main(String[] args) { // create two Sphere objects Sphere ball = new Sphere(4.0, "ball"); Sphere orange = new Sphere(4.0, "orange"); // compute volume of the ball double ballVol = ball.getVolume(); // print a description of the orange System.out.println(orange); }// end main }// class Calculation public class Sphere { private double radius; // radius of sphere private String type ; // what the sphere is // Constructor: assign values to the two instance variables of the sphere public Sphere(double r, String t) { radius = r; type = t; }// end constructor // instance method getVolume for calculating volume of the Sphere public double getVolume() { return (4./3*Math.PI*Math.pow(radius, 3)); } // define a toString method for showing the type and radius of the Sphere public String toString() { return ("The object is " + type + " with radius " + radius); } }// class Sphere