Question

In: Computer Science

Java Q1: Create a class named Triangle, the class must contain: Private data fields base and...

Java

Q1: Create a class named Triangle, the class must contain:

  • Private data fields base and height with setter and getter methods.
  • A constructor that sets the values of base and height.
  • A method named toString() that prints the values of base and height.
  • A method named area() that calculates and prints the area of a triangle.

  • Draw the UML diagram for the class.
  • Implement the class.

Q2: Write a Java program that creates a two-dimensional array of type integer of size x by y (x and y must be entered by the user). The program must fill the array with random numbers from 1 to 100. The program must prompt the user to enter a search key between 1 and 100. Then, the program must check whether the search key is available in the array and print its location.

Here is a sample run:

Enter the number of rows: 5

Enter the number of columns: 5

Enter a search key between 1-100: 27

The key you entered is available at row 3, column 2.

Here is another sample run:

Enter the number of rows: 5

Enter the number of columns: 5

Enter a search key between 1-100: 33

The key you entered is not available in the array.

Solutions

Expert Solution

Q1:

import java.util.*;
public class Triangle {
private double base,height;
public void setBase(double b) {
   base = b;
}
public void setHeight(double h) {
   height = h;
}
public double getBase() {
   return(base);
}
public double getHeight() {
   return(height);
}
public Triangle(double b,double h) {
   base = b;
   height = h;
}
public void toMyString() {
   System.out.println("Base of the triangle: "+base+"\nHeight of the triangle: "+height);
}
public void area() {
   System.out.println("area of the triangle: "+0.5*base*height);
}
}
public class TriangleMain {
public static void main(String args[]) {
   Triangle t1 = new Triangle(7,8);
   t1.toMyString();
   t1.area();
}
}

Output

Q2:

import java.util.*;
public class Array {
   public static int[] searchArray(int[][] a,int x,int y,int n) {
       for(int i=0;i<x;i++) {
       for(int j=0;j<y;j++) {
           if(a[i][j] == n)
               return new int[] {i,j};               
       }        
   }
       return null;
   }
public static void main(String args[]) {
   int x,y,search;
   Scanner s = new Scanner(System.in);
   Random r = new Random();
   System.out.print("Enter the number of rows: ");
   x = s.nextInt();
   System.out.print("Enter the number of columns: ");
   y = s.nextInt();
   int [][]array = new int[x][y];
   for(int i=0;i<x;i++) {
       for(int j=0;j<y;j++) {
           array[i][j] = r.nextInt(100-1)+1;
       }
   }
   /* To print the array
   for(int i=0;i<x;i++) {
       for(int j=0;j<y;j++) {
           System.out.print(array[i][j]+" ");
       }
       System.out.println();
   }
   */
   System.out.print("Enter a search key between 1-100: ");
   search = s.nextInt();
   int[] values = new int[2];
   values = searchArray(array,x,y,search);
   if (values != null)
       System.out.println("The key you entered is available at row "+(values[0]+1)+",column "+(values[1]+1));
   else
       System.out.println("The key you entered is not available in the array");
}
}


Related Solutions

Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for the following: 1. Name the class SalonServices 2. Add private data fields: a. salonServiceDescription – This field is a String type b. price - This field is a double type 3. Create two methods that will set the field values. a. The first method setSalonServiceDescription() accepts a String parameter defined as service and assigns it to the salonServiceDescription. The method is not static b....
This is 1 java question with its parts. Thanks! Create a class named Lease with fields...
This is 1 java question with its parts. Thanks! Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
Java Beginner a)Create a class named Student that has fields for an ID number, number of...
Java Beginner a)Create a class named Student that has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field for grade point average. Include a method to compute the grade point average...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well...
INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT