Friday, January 5, 2018

Working with Classes (3-Methods)

Challenge 1: Write a printDate method that displays the current date. No value to be passed or returned. Test the method by invoking it from the class constructor.
Note: To obtain the current date, instantiate an object variable of type java.util.Date and print it. 




import java.util.*;

class lab10_01 {
    public static void main(String[] args) {
        GetDate newDate = new GetDate();
    }
}
class GetDate {

    Date date = new Date();

    public GetDate() {
        printDate();
    }

    void printDate() {
        System.out.printf("%1$s %2$tB %2$td, %2$tY", "Date:", date);
        System.out.println("");
    }
}

Challenge 2: Write a getChar method that asks the user to enter a character and then returns it. Test your method. 

import java.util.*;

class lab10_02 {

    public static void main(String[] args) {   
    GetChar newChar = new GetChar();
    }        
}

class GetChar {
    public GetChar() {
        System.out.println("The char entered was: " + myChar());
    }

   public static char myChar(){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a Char");
        char myChar = in.next().charAt(0);
        return myChar;
   }

}

Challenge 3: Write a rectangleArea, squareArea, and circleArea methods that calculate and print the area of a rectangle, a square, and a circle, respectively. These methods will have the following signature:
public double rectangleArea(double h, double w)
public double squareArea(double s)public double circleArea(double r)
x


public void rectangleArea(double h, double w)

(h and w are the height and width of the rectangle, respectively) 
public void squareArea(double s) (s is the length of the side of the square)
public void circleArea(double r) (r is the radius of the circle)
Test your methods. Do not create three different projects; just add the three methods to the same class. 

import java.util.Scanner;

public class lab10_03 {

    public static void main(String[] args) {
        GetArea myArea = new GetArea();
        Scanner in = new Scanner(System.in);

        System.out.println("1. Rectangle ");
        System.out.println("2. Square");
        System.out.println("3. Circle");
        System.out.println("4. Exit");

        int choice = in.nextInt();

        if (choice == 1) {
            System.out.println("Enter the length and width of your rectangle: ");
            double h = in.nextDouble();
            double w = in.nextDouble();
            myArea.retangleArea(h, w);




            System.out.println("\n");
        } else if (choice == 2) {
            System.out.println("Enter one dimension of the Square");
            double s = in.nextDouble();
            myArea.square(s);
            System.out.println("\n");
        } else if (choice == 3) {
            System.out.println("Enter one dimension of the Circle");
            double r = in.nextDouble();
            myArea.circleArea(r);
            System.out.println("\n");
        } else if (choice == 4) {
            System.out.println("Have a good one!");
        } else {
            System.out.println("Input Error!");
        }
    }
}
class GetArea {

    Scanner in = new Scanner(System.in);

    public void retangleArea(double h, double w) {

        double area = h * w;
        System.out.println("The area of the rectangle is: " + area);
    }

    public void square(double s) {

        double square = Math.pow(s, 2);
        System.out.printf("The area of the square is: %.2f", square);
    }
    public void circleArea(double r) {
        double area = Math.PI * Math.pow(r, 2);
        System.out.printf("The area of the circle is: %.2f", area);
    }


}

Challenge 4: (This exercise is a variation of the previous one) Write a rectangleArea, squareArea, and circleArea methods that calculate and return the area of a rectangle, a square, and a circle, respectively. These methods will have the following signature:
Test your methods by invoking them from the class constructor and printing in the constructor the values returned. 
import java.util.Scanner;

public class lab10_04 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        
        System.out.println("1. Rectangle ");
        System.out.println("2. Square");
        System.out.println("3. Circle");
        System.out.println("4. Exit");

        int choice = in.nextInt();
        if (choice == 1) {
          System.out.println("Enter the length and width of your rectangle: ");
            double w = in.nextDouble();
            double h = in.nextDouble();
            
            GetAreas rectangleArea =new GetAreas (w,h);
            
        } else if (choice == 2) {
         System.out.println("Enter one dimension of the square");
        double s = in.nextDouble();    
        GetAreas squareArea  = new GetAreas(s);
        System.out.printf("The area of the square is: %.2f", squareArea);
            System.out.println();    
            
        } else if (choice == 3) {
          GetAreas myArea = new GetAreas();
          System.out.println();
            
        } else if (choice == 4) {
            System.out.println("Have a good one!");
        } else {
            System.out.println("Input Error!");
        }
    }
}

class GetAreas {

    Scanner in = new Scanner(System.in);

    public GetAreas() {

        System.out.println("Circle area is: " + circleArea());

    }

    public GetAreas(double x) {
        square(x);
    }

    public GetAreas(double x, double y) {
        rectangleArea(x, y);
        System.out.println("The area of the rectangle is: " + rectangleArea(x,y));
    }
    public double rectangleArea(double h, double w) {
   
        return h * w;
    }
    public double square(double s) {
  
        return Math.pow(s, 2);
    }





    public double circleArea() {
        System.out.println("Enter the radius of the circle");
        double r = in.nextDouble();
        return Math.PI * Math.pow(r, 2);
       
    }

}

No comments:

Post a Comment