In: Computer Science
//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