Please Answer this worksheet, This is for an Advaned Java Course! Lesson Objecti
ID: 3850496 • Letter: P
Question
Please Answer this worksheet, This is for an Advaned Java Course!
Lesson Objectives:
• Create a custom generic class
• Use the type interface diamond to create an object
• Use generic methods
• Use wildcards
• Use enumerated types
Vocabulary:
Identify the vocabulary word for each definition below.
Try It/Solve It:
1. Create a generic class called Cuboid that will store the three dimensions of a cuboid. Add methods to set and get
the length, breadth and Height. Add a method public String toString() that will return all of the dimensions. The type
of the dimensions will be decided at construction of the cuboid instance. Example:
Cuboid c1 = new Cuboid<>();
Cuboid c1 = new Cuboid<>();
2. Modify your generic class Cuboid so that it only accepts Numbers. Add a method with the following definition and
complete its body. Hint: Look at the method doubleValue() in the Number class.
public double getVolume(){ …… }
3. A calendar expert, Mr. Dayes, wishes to make a class that will be used to initialize and store the days of the week.
Would it be useful for him to use an enum type? Why or why not?
4. Create a generic class that stores a Key and a Value and call it Map. This will have one method called toString that
will return a string that shows the key and the value. The key and the value can be of any type. Test your class by
adding the following three years and an associated iconic car to a separate instance of each. You will end up with
three instances of Map storing the following data:
Explanation / Answer
1)Code for the 1st question:
class Cuboid<G>
{
G lenght,width,height;
//Setter and getter methods
public G getLenght()
{
return lenght;
}
public void setLenght(G lenght)
{
this.lenght = lenght;
}
public G getWidth()
{
return width;
}
public void setWidth(G width)
{
this.width = width;
}
public G getHeight()
{
return height;
}
public void setHeight(G height)
{
this.height = height;
}
//Constructor to initialize objects
public Cuboid(G lenght, G width, G height)
{
super();
this.lenght = lenght;
this.width = width;
this.height = height;
}
//toString method
public String toString()
{
return "Cuboid [lenght=" + lenght + ", width=" + width + ", height=" + height + "]";
}
}
public class CuboidTest
{
public static void main(String args[]) {
// Create a Generic reference for Integers.
Cuboid<Integer> iOb = new Cuboid<Integer>(88, 10, 30);
System.out.println(iOb.toString());
//Create a Generic object for Strings.
Cuboid<String> strOb = new Cuboid<String>("Generics Test","Second String","Third String");
System.out.println(strOb.toString());
}
}
Ouput:
Cuboid [lenght=88, width=10, height=30]
Cuboid [lenght=Generics Test, width=Second String, height=Third String]
2)Code for 2nd question
class Cuboid<Double,Double1>
{
Double1 lenght,width,height;
public Cuboid(Double1 lenght, Double1 width, Double1 height) {
super();
this.lenght = lenght;
this.width = width;
this.height = height;
}
public Double1 getLenght()
{
return lenght;
}
public void setLenght(Double1 lenght) {
this.lenght = lenght;
}
public Double1 getWidth() {
return width;
}
public void setWidth(Double1 width) {
this.width = width;
}
public Double1 getHeight() {
return height;
}
public void setHeight(Double1 height) {
this.height = height;
}
public double getVolume(double lenght,double width,double height)
{
return lenght*width*height;
}
@Override
public String toString() {
return "Cuboid [lenght=" + lenght + ", width=" + width + ", height=" + height + "]";
}
}
public class CuboidTest
{
public static void main(String args[]) {
// Create a Generic reference for Double.
Cuboid<Double,Double> iOb = new Cuboid<Double,Double>(88.50, 10.4, 50.23);
System.out.println(iOb.toString());
Cuboid ob= new Cuboid(1,2,3);
System.out.println(ob.getVolume(10.12, 12.2, 13.2));
}
}
Ouput for the 2nd Question:
Cuboid [lenght=88.5, width=10.4, height=50.23]
1629.7247999999997
3)Code for 3rd Question:
class EnumerationExample
{
public enum Days {SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY}
public static void main(String[] args)
{
for (Days s : Days.values())
System.out.println(s);
}
}
Ouput for 3rd question:
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
4)Code for 4th question:
import java.util.*;
class TestMap
{
public static void main(String args[])
{
//Map to store key and value
Map<Integer,String> map=new HashMap<Integer,String>();
map.put(1989,"GT-R");
map.put(1969,"Capri");
map.put(1953,"Corvette C1");
//Now use Map.Entry for Set and Iterator
Set<Map.Entry<Integer,String>> set=map.entrySet();
Iterator<Map.Entry<Integer,String>> itr=set.iterator();
while(itr.hasNext())
{
Map.Entry e=itr.next();//no need to typecast
System.out.println(e.getKey()+" "+e.getValue());
}
}
}