Question

In: Computer Science

programming language: JAVA 4. For this question, write code fragments or methods as directed. (a) Create...

programming language: JAVA

4. For this question, write code fragments or methods as directed. (a) Create the Point class that depends on a generic data type parameter T. It has two instance variables called xCoordinate and yCoordinate that both have type T. Write a two parameter constructor to initialize the instance variables. Answer: (b) Write a main method that has instructions to perform the following tasks (in order): Declare an ArrayList of Strings called carModels. Add three models. Print the size of the list. Remove the model from index 1. Insert a model at index 0. Replace the model at index 2. Answer: (c) Write a main method with a try-catch block. Inside the block it creates an array of Strings — the array is called list and has size 10. Print the value stored in list[10]. The catch block should catch ArithmeticException and RuntimeException. Your code shouldn’t be longer than 9 lines excluding curly braces. Answer: (d) Write the private method inputCircles that is called by the following main method. As- sume that class Circle exists and has constructors and a toString method. Your method should open a Scanner and obtain a radius for each of the 20 circles that you make from the user. (You should assume that the class Scanner has already been imported.) public static void main(String args[]) { Circle[] data = new Circle[20]; inputCircles(data); for (Circle c:data) System.out.println(c); } Answer:

Solutions

Expert Solution

(a)
//Point.java
/*Generic Point class of data type T
* The constructor takes two generic
* types,T and then set x and y values
* to x coordinate and ycoordinate*/
public class Point<T>
{
   private T xCoordinate;
   private T yCoordinate;
   /*constructor */
   public Point(T x, T y)
   {
       xCoordinate=x;
       yCoordinate=y;
   }
} //end of Point class

------------------------------------------------------------------------------------------------

(b)
import java.util.ArrayList;
public class Exercise_2
{
   public static void main(String[] args)
   {
       //Declare an ArrayList of Strings called carModels.
       ArrayList<String>carModels=new ArrayList<String>();
       //Add three models.
       carModels.add("Tata");
       carModels.add("Ferari");
       carModels.add("Maruthi");
       //Print the size of the list.
       System.out.println("Size of the carModels list : "+carModels.size());
       //Remove the model from index 1.
       carModels.ensureCapacity(1);
       //Insert a model at index 0.
       carModels.set(0,"Kia");
       //Replace the model at index 2.
       carModels.set(2, "Ford");
   }
}

------------------------------------------------------------------------------------------------

(c)


//Java program that demonstrates the (c) bit
//Exercise_3.java
public class Exercise_3
{
   public static void main(String[] args)
   {
       //Inside the block it creates an array of Strings
       String list[]=null;
       try
       {
           //the array is called list and has size 10.
           list=new String[10];
           //Print the value stored in list[10].
           for (int i = 0; i < list.length; i++)
               System.out.println(list[i]);          
       }
       //The catch block should catch ArithmeticException and RuntimeException.
       catch (ArithmeticException e)
       {
           System.out.println(e.getMessage());
       }
       catch (RuntimeException e)
       {
           System.out.println(e.getMessage());
       }
   }
}

Sample Output:
null
null
null
null
null
null
null
null
null
null

------------------------------------------------------------------------------------------------
(d)

//Circle.java
public class Circle
{
   private int radius;
   /*constructor*/
   public Circle()
   {
       radius=0;
   }
   /*constructor with radius*/
   public Circle(int r)
   {
       radius=r;
   }
   //set radius
   public void setRadius(int radius)
   {
       this.radius=radius;
   }
   //return radius as string
   public String toString()
   {  
       return String.format("Radius: %d", radius);
   }
}


/*Java program to demonstrate the Circle class
* in below program .
* Program prompt user to enter 20 radius values
* and the print the user enter values on console*/
//Exercise_4.java
import java.util.Scanner;
public class Exercise_4
{
   public static void main(String[] args)
   {
       //create an array of Circle class of size,20
       Circle[] data = new Circle[20];
       //calling inputCircles
       inputCircles(data);
       //print the circle objects
       for (Circle c:data)
           System.out.println(c);
   }

   private static void inputCircles(Circle[] data)
   {
       Scanner console=new Scanner(System.in);
       for (int r = 0; r < data.length; r++)
       {
           System.out.printf("Enter radius,r :");
           int radius=Integer.parseInt(console.nextLine());
           data[r]=new Circle(radius);
       }
   }
}


Sample output:

Enter radius,r :1
Enter radius,r :2
Enter radius,r :3
Enter radius,r :4
Enter radius,r :5
Enter radius,r :6
Enter radius,r :7
Enter radius,r :8
Enter radius,r :9
Enter radius,r :10
Enter radius,r :11
Enter radius,r :12
Enter radius,r :13
Enter radius,r :14
Enter radius,r :15
Enter radius,r :16
Enter radius,r :17
Enter radius,r :18
Enter radius,r :19
Enter radius,r :20
Radius: 1
Radius: 2
Radius: 3
Radius: 4
Radius: 5
Radius: 6
Radius: 7
Radius: 8
Radius: 9
Radius: 10
Radius: 11
Radius: 12
Radius: 13
Radius: 14
Radius: 15
Radius: 16
Radius: 17
Radius: 18
Radius: 19
Radius: 20


Related Solutions

The programming language is Java. Write the code you would use to fill the variable cubed...
The programming language is Java. Write the code you would use to fill the variable cubed with the value stored in x to the third power.
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
Java language (a) Write code segments to perform the following: (i) declare and create an integer...
Java language (a) Write code segments to perform the following: (i) declare and create an integer array freqArray of size 8 (ii) declare and initialize an array weight (with suitable type) which contains 48.5, 80 and 68 (iii) declare a Mouse array of size 2 with name mouse and initialize it with Mouse objects using one statement (b) A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3};...
Java Programming In this assignment we are going to create our own programming language, and process...
Java Programming In this assignment we are going to create our own programming language, and process it Java. programming language has 6 commands enter add subtract multiply divide return enter, add, subtract, multiply, divide all take 1 parameter (a double value). return takes no parameters.
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement for a salesperson at a rate of $0.35 per mile. Your program should interact (ask the user to enter the data) with the user in this manner: MILEAGE REIMBURSEMENT CALCULATOR Enter beginning odometer reading > 13505.2 Enter ending odometer reading > 13810.6 You traveled 305.4 miles. At $0.35 per mile, your reimbursement is $106.89. ** Extra credit 6 points: Format the answer (2 points),...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner object to read input. int Age;     Write a java statement to read the Age input value 4 . Redo 1 to 3 using JOptionPane
Language: Java To be able to code a class structure with appropriate attributes and methods. To...
Language: Java To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure to...
Java programming language Write a Java application that simulates a test. The test contains at least...
Java programming language Write a Java application that simulates a test. The test contains at least five questions. Each question should be a multiple-choice question with 4 options. Design a QuestionBank class. Use programmer-defined methods to implement your solution. For example: - create a method to simulate the questions – simulateQuestion - create a method to check the answer – checkAnswer - create a method to display a random message for the user – generateMessage - create a method to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT