Java Week - 13
Java Week - 13
Date:……….
WEEK-13
1) Develop the logic for the following scenario. Consider the class Cuboid with length, breadth,
and height as private attributes. Code the parameterized constructor and toString() method.
Develop main() method that stores data of Cuboids as a collection and must be menu driven with
the following operations: 1. Add new Cuboid, 2) Sort by length, 3) Sort by Volume.
[HINT:Volume of cuboid is length*breadth*height].
Class Diagram:
Program:
package week13;
}
package week13;
import java.util.Comparator;
@Override
public int compare(Cuboid o1, Cuboid o2) {
return (int)(o1.getLength()-o2.getLength());
}
}
package week13;
import java.util.Comparator;
@Override
public int compare(Cuboid o1, Cuboid o2) {
return (int)(o1.volume()-o2.volume());
}
}
package week13;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
case 4:
Collections.sort(list,new sortByVolume());
for(Cuboid c: list) {
System.out.println(c);
}
break;
case 5:
System.exit(1);
break;
default:
System.out.println("Enter a valid choice");
}
}
}
}
Output:
2) Write a program that prompts the user to enter a text file name and displays the number of
vowels and consonants in the file. Use a set to store the vowels A, E, I, O, and U(Hint: Use
HashSet).
Class Diagram:
Program:
package lab13_1;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
}
}
System.out.println("Vowel count: "+vowels);
System.out.println("Consonant count: "+consonants);
input.close();
}
}
Output:
3) Write a program that reads an unspecified number of integers and finds the one that has the
most occurrences. The input ends when the input is 0. For example, if you entered 2 3 40 3 5 4 –
3 3 3 2 0, the number 3 occurred most often. If not one but several numbers have the most
occurrences, all of them should be reported. For example, since 9 and 3 appear twice in the list 9
30 3 9 3 2 4, both occurrences should be reported.(Hint:Use HashMap).
Class Diagram:
Program:
package lab13_1;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
while(true) {
if(key==0)
break;
if(map.containsKey(key)) {
int value=map.get(key);
value++;
map.put(key, value);
else
map.put(key, 1);
int max=Collections.max(map.values());
set.addAll(map.keySet());
for(Integer i: set) {
if(map.get(i)== max)
System.out.print(i+" ");
sc.close();
Output: