Question

In: Computer Science

Both in Java Question 1: Write a method, bunnyEars that takes in a integer for a...

Both in Java
Question 1:
Write a method, bunnyEars that takes in a integer for a number of bunnies and return another integer for the total number of ears that the group of bunnies has. (Assume ear bunny has exactly 2 ears)..
Write a method countX, that when given a string counts the number of lowercase 'x' chars in the string.
countX("xxhixx") → 4
countX("xhixhix") → 3
countX("hi") → 0

Write a method copies that, when given a string and a non-empty substring sub, compute recursively if at least n copies of sub appear in the string somewhere, possibly with overlapping. N will be non-negative.
strCopies("catcowcat", "cat", 2) → true
strCopies("catcowcat", "cow", 2) → false
strCopies("catcowcat", "cow", 1) → true


Question 2:

Shape Interface
Create an interface called Shape.java
Give it two methods: area, perimeter. Each should return a double.
Rectangle
Create a concrete class Rectangle that implements Shape.
Give it a height, width, and a constructor that sets both of them.
The area of a rectangle is height X width
The perimeter of a rectangle is 2 * height + 2 * width
Square
Create a concrete class Square that implements Shape.
Give it a width, and a constructor that sets it.
The area of a square is width X width
The perimeter of a circle is width + width + width + width
Triangle
Create a concrete class triangle that implements Shape.
Give it a length for 3 sides and a constructor that sets them
The area of a triangle is 1/2 base * height
The perimeter of a triangle the sum of its sides
Equilateral Triangle
Create a concrete class EquilateralTriangle that implements Shape.
Give it a length for 1 sides and a constructor that sets all of its sides to it.
The area of a triangle is 1/2 base * height
The perimeter of a triangle the sum of its sides
Circle
Create a concrete class Circle that implements Shape.
Give it a value radius and a constructor that sets it.
The area of a circle is 2 * pi * radius
The perimeter of a circle is pi * radius * radius

Solutions

Expert Solution

//Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter number of Bunnies: ");
        int n = input.nextInt();
        System.out.println(n+" bunnies have "+bunnyEars(n)+" ears.");
    }
    private static int bunnyEars(int numberOfBunnies)
    {
        return 2*numberOfBunnies;
    }
}

//Output

//=============================================

import java.util.Scanner;

public class Main {
    public static void main(String[] args)
    {
        countX("xxhixx") ;
        countX("xhixhix") ;
        countX("hi") ;
        System.out.println("countX(\"xxhixx\") → "+countX("xxhixx")+"\n"
               +"countX(\"xhixhix\") → "+countX("xhixhix")+"\n"+
               "countX(\"hi\") → "+countX("hi")
        );

    }
    private static int countX(String s)
    {
        int count =0;
        for (int i = 0; i <s.length() ; i++) {
            if(s.charAt(i)=='x')
                count++;
        }
        return count;
    }
}

//Output

//======================================

import java.util.Scanner;

public class Main {
    public static void main(String[] args)
    {
        boolean b1 = strCopies("catcowcat", "cat", 2) ;
        boolean b2 = strCopies("catcowcat", "cow", 2) ;
        boolean b3 = strCopies("catcowcat", "cow", 1) ;
        System.out.println(b1+"\n"+b2+"\n"+b3);
    }
    private static boolean strCopies(String string, String subStr, int n)
    {
        if (string.isEmpty())
            return n == 0;
        int remainingN = string.startsWith(subStr) ? n - 1 : n;
        return strCopies(string.substring(1), subStr, remainingN);
    }
}

//Output

//Solution2

//Java code

public interface Shape {
    public double area();
    public double perimeter();
}

//============================

public class Rectangle implements Shape {
    private double height;
    private double width;
    //Constructor

    public Rectangle(double height, double width) {
        this.height = height;
        this.width = width;
    }

    @Override
    public double area() {
        return height*width;
    }

    @Override
    public double perimeter() {
        return 2*(height+width);
    }

    @Override
    public String toString() {
        return "Area of Rectangle: "+area()+"\nPerimeter of Rectangle: "+perimeter();
    }
}

//========================

public class Square implements Shape {
    private double width;
    //Constructor

    public Square(double width) {
        this.width = width;
    }

    @Override
    public double area() {
        return width*width;
    }

    @Override
    public double perimeter() {
        return 4*(width);
    }
    @Override
    public String toString() {
        return "Area of Square: "+area()+"\nPerimeter of Square: "+perimeter();
    }
}

//============================

public class Triangle implements Shape {
    private double side1,side2,side3;

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }


    @Override
    public double area() {
        return 0.5*side1*side2;
    }

    @Override
    public double perimeter() {
        return side1+side2+side3;
    }
    @Override
    public String toString() {
        return "Area of Triangle: "+area()+"\nPerimeter of Triangle: "+perimeter();
    }
}

//===============================

public class EquilateralTriangle implements Shape {
    private double side1,side2,side3;
    public EquilateralTriangle(double side)
    {
        side1= side;
        side2 = side;
        side3 = side;
    }
    @Override
    public double area() {
        return 0.5*side1*side2;
    }

    @Override
    public double perimeter() {
        return side1+side2+side3;
    }
    @Override
    public String toString() {
        return "Area of EquilateralTriangle: "+area()+"\nPerimeter of EquilateralTriangle: "+perimeter();
    }
}

//============================

public class Circle implements Shape {
    private double radius;
    //Constructor

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double area() {
        return Math.PI*radius*radius;
    }

    @Override
    public double perimeter() {
        return 2*Math.PI*radius;
    }
    @Override
    public String toString() {
        return "Area of Circle: "+area()+"\nPerimeter of Circle: "+perimeter();
    }
}

//================================

public class ShapeTest {
    public static void main(String[] args)
    {
        Shape shape1 = new Rectangle(4,5);
        Shape shape2 = new Square(5);
        Shape shape3 = new Triangle(4,5,6);
        Shape shape4 = new EquilateralTriangle(5);
        Shape shape5 = new Circle(7);
        //Print info
        System.out.println(shape1);
        System.out.println(shape2);
        System.out.println(shape3);
        System.out.println(shape4);
        System.out.println(shape5);

    }
}

//Output

//I fyou need any help regarding this solution........ please leave a comment..... thanks


Related Solutions

1 Write a Java method that takes an integer, n, as input and returns a reference...
1 Write a Java method that takes an integer, n, as input and returns a reference to an array of n random doubles between 100.0 and 200.0. Just write the method. 2. You have a class A: public class A { int i, double d; public A(double d) { this.d=d; this.i=10; } }
// Java // This method takes an integer array as well as an integer (the starting...
// Java // This method takes an integer array as well as an integer (the starting // index) and returns the sum of the squares of the elements in the array. // This method uses recursion. public int sumSquaresRec(int[] A, int pos) { // TODO: implement this method        return -1; // replace this statement with your own return }    // This method takes a character stack and converts all lower case letters // to upper case ones....
Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer...
Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer values squared and reversed. Example: if the LinkedList has (9, 5,4,6), then the returned list will have (36, 16,25,81). What is the time-complexity of your code? You must use listIterator for full credit. public LinkedList getReverseSquaredList (LinkedList list) { }
Write a java code that: 1) Takes as an argument an integer number, say N 2)...
Write a java code that: 1) Takes as an argument an integer number, say N 2) Creates an array of size N 3) Populates the array with random numbers between 0 and 10 * N. This is, the values of the elements of the array should be random numbers between 0 and 10 * N. 4) Finally, the code outputs the index of the smallest element and the index of the largest element in the array
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and returns the expanded ArrayList.  The total size will be (k+1) * n.   public ArrayList<Integer> makeCopies(ArrayList<Integer>, int k) { } Example: ArrayList<Integer> has (3,7,4) and k = 2, then the returned, expanded ArrayList will have (3,7,4,3,7,4,3,7,4).  The total size is (k+1)*n = (2+1)*3= 9.
Java Write a valid Java method called printReverse that takes a String as a parameter and...
Java Write a valid Java method called printReverse that takes a String as a parameter and prints out the reverse of the String. Your method should not return anything. Make sure you use the appropriate return type.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT