java pta exercises Chapter 4

6-1 create a right triangle class to implement IShape interface (10 points)

Create a right triangle RTriangle class to implement the following interface IShape. The length of two right angle edges is the private member of RTriangle class, which contains the construction method with right angle edges as parameters.

interface IShape {// Interface
public abstract double getArea(); // Abstract method for calculating area
public abstract double getPerimeter(); // Abstract method for perimeter
}

###Definition of right triangle class:
The constructor prototype of right triangle class is as follows:
RTriangle(double a, double b);

import java.util.Scanner;
import java.text.DecimalFormat;

interface IShape {
    public abstract double getArea();

    public abstract double getPerimeter();
}

/*The code you write will be embedded here*/


public class Main {
    public static void main(String[] args) {
        DecimalFormat d = new DecimalFormat("#.####");
        Scanner input = new Scanner(System.in);
        double a = input.nextDouble();
        double b = input.nextDouble();
        IShape r = new RTriangle(a, b);
        System.out.println(d.format(r.getArea()));
        System.out.println(d.format(r.getPerimeter()));
        input.close();
    }
}

answer

class RTriangle implements IShape{
	private double a,b;
	public RTriangle(double a, double b) {
		this.a = a;
		this.b = b;
	}
	public double getArea() {
		return 0.5*a*b;
	}
	public double getPerimeter() {
		return a+b+Math.sqrt(a*a+b*b);
	}
}

6-2 extend a circular class circle from the abstract class shape class (10 points)

Please extend a circular class Circle from the following abstract class shape class. The radius of this class Circle is a private member, and the class should contain the construction method of initializing the radius.

public abstract class shape {// abstract class
public abstract double getArea();// Find area
public abstract double getPerimeter(); // Find perimeter
}

The main class inputs the radius value of the circle from the keyboard, creates a circular object, and then outputs the area and perimeter of the circle. Keep 4 decimal places.
Circular class name Circle

import java.util.Scanner;
import java.text.DecimalFormat;

abstract class shape {// abstract class
     /* Abstract method for calculating area */
    public abstract double getArea( );

    /* Abstract method for perimeter */
    public abstract double getPerimeter( );
}

/* The code you submit will be embedded here */

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        DecimalFormat d = new DecimalFormat("#.####"); / / keep 4 decimal places
         double r = input.nextDouble( ); 
        shape c = new  Circle(r);

        System.out.println(d.format(c.getArea()));
        System.out.println(d.format(c.getPerimeter()));
        input.close();
    } 
}

answer

class Circle extends shape
{
	double radius;
	final static double PI = 3.1415926535;
	public Circle(double radius) {
		this.radius = radius;
	}
	@Override
	public double getArea() {
		// TODO Auto-generated method stub
		return PI*radius*radius;
	}
	@Override
	public double getPerimeter() {
		// TODO Auto-generated method stub
		return 2*PI*radius;
	}	
}

6-20 find the area and perimeter of the positive n-shape (10 points)

In a Regular Polygon, the side lengths of all sides are equal and the degrees of all corners are the same (that is, the polygon is equilateral and equiangular). We have implemented a Regular Polygon class Regular Polygon from the following interface IShape. Its construction method is: Regular Polygon (int n, double side); where n is the number of sides and side is the side length.

Input the number of sides N and side length a of the positive n-sided shape from the keyboard. Please program to calculate the area and perimeter of the positive n-sided shape.

interface IShape {// Interface
double getArea(); // Find area
double getPerimeter();// Find perimeter
}
interface IShape {// Interface

double getArea(); // Find area

double getPerimeter();// Find perimeter

}
/* Here is the implementation of the regular polygon class of positive n-sided shape*/

public class Main {
    public static void main(String[] args) {

        /* The code you submit will be embedded here */ 

    }
}
### Input example:

A set of inputs is given here. For example:

```in
5
7

answer

        Scanner input = new Scanner(System.in);
        DecimalFormat d = new DecimalFormat("#.####");//  Keep 4 decimal places
        int n=input.nextInt();
        double side = input.nextDouble();
        double area,Perimeter;
        shape rp = new  RegularPolygon(n,side);
        area=n*side*side/(Math.tan(Math.toRadians(180/n))*4);
        Perimeter=n*side;
        System.out.println(d.format(area));
        System.out.println(d.format(Perimeter));
        input.close();

7-1 find the largest object (10 points)

(find the largest object) write a method to return the largest object in the object array. The method signature is as follows:

public static Object max(Comparable[] a)

All objects are instances of the Comparable interface. The order of objects in the array is determined by the compareTo method.

Write a test program, input 5 strings and 5 integers from the keyboard, and create an array composed of 5 strings and an array composed of 5 integers. Find the largest string and integer in the array and output them.

import java.util.*;
public class Main {
public static Object max(Comparable[] a)
{
        Arrays.sort(a);
        return a[4];
}

answer

public static void main(String[]args)
{
    Scanner in=new Scanner(System.in);
    Comparable<String>[] a=new Comparable[5];
    Comparable<Integer>[] b=new Comparable[5];
    for(int i=0;i<5;i++)
        a[i]=in.next();
    for(int i=0;i<5;i++)
        b[i]=in.nextInt();
    System.out.println("Max string is "+max(a));
    System.out.println("Max integer is "+max(b));
    in.close();
    }
}

Keywords: Java Back-end

Added by Vebut on Fri, 22 Oct 2021 15:39:05 +0300