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