a. Write an abstract class called File that has 2 attributes: filename and size
ID: 3572950 • Letter: A
Question
a. Write an abstract class called File that has 2 attributes: filename and size (in MB). It has an abstract method called view. The method view typically returns a string that describes the object.
b. Write an interface Playable that has one method play.
c. Write a class Document that inherits File and has an attribute: length (number of characters).
d. Write a class Video that inherits File and has an attribute: type (mpeg, avi, mp4, etc.) and duration (number of minutes). It implements Playable by printing both the name of the player application and the name of the file.
e. Write a class Audio that inherits File and has two attributes: type (wav, mp3, etc.) and duration (number of seconds). It implements Playable by printing both the name of the player application and the name of the file.
f. Write a static method view in File that takes an array list of files and views each of them. g. Write a static method isPlayable in File that takes a file and checks if it is playable or not. h. Write a static method play in File that takes an array list of files and only play any file that is playable.
i. Write a static method getVideos in File that takes two array lists of files and returns an array list that has all videos in both lists without repetition.
Q2. [4 points] Complete the following class: public class MyIntArray implements Comparable{ public int[] a = null; // ..................... } where MyIntArray x is smaller than/greater than/equal to MyIntArray y if summation of values of elements in x is less than/greater than/equal to summation of values of elements in y.
Explanation / Answer
ANSWERS:
File.java
package sample;
import java.util.ArrayList;
//Question a)
public abstract class File {
String filename;
double size;
public abstract String view();
//Question f)
public static void view(ArrayList<File> files)
{
for(int i=0; i<files.size(); i++)
{
System.out.println(files.get(i).filename);
System.out.println(files.get(i).size + " MB");
}
}
//Question g)
public static boolean isPlayable(File file)
{
if(file.getClass().toString().contains("Audio") ||
file.getClass().toString().contains("Video"))
return true;
else return false;
}
///Question h)
public static void play(ArrayList<File> files)
{
for(int i=0; i<files.size(); i++)
{
if(isPlayable(files.get(i)))
{
files.get(i).play();
}
}
}
//Question i)
public static ArrayList<File> getVideos(ArrayList<File> files1, ArrayList<File> files2)
{
ArrayList<File> listOfFiles = new ArrayList<File>();
files1.addAll(files2);
for(int i=0; i<files1.size(); i++)
{
if(!listOfFiles.contains(files1.get(i)))
listOfFiles.add(files1.get(i));
}
return listOfFiles;
}
}
Playable.java
package sample;
//Question b)
public interface Playable {
public void play();
}
Document.java
package sample;
//Question c)
public class Document extends File{
int length;
@Override
public String view() {
// TODO Auto-generated method stub
return null;
}
}
Video.java
package sample;
//Question d)
public class Video extends File implements Playable{
String type;
int duration;
@Override
public void play() {
System.out.println("Name of the player application: Video");
System.out.println("Name of the file: " + filename);
}
@Override
public String view() {
// TODO Auto-generated method stub
return null;
}
}
Audio.java
package sample;
//Question e)
public class Audio extends File implements Playable{
String type;
int duration;
@Override
public void play() {
System.out.println("Name of the player application: Audio");
System.out.println("Name of the file: " + filename);
}
@Override
public String view() {
// TODO Auto-generated method stub
return null;
}
}
MyIntArray.java
package sample;
//Question Q2)
public class MyIntArray implements Comparable{
public int[] a = null;
@Override
public int compareTo(Object o) {
MyIntArray y = (MyIntArray)o;
int thisTotal = 0;
int oTotal = 0;
for(int i=0; i<this.a.length; i++)
{
thisTotal += this.a[i];
}
for(int j=0; j<y.a.length; j++)
{
oTotal += y.a[j];
}
if(thisTotal == oTotal)
return 0;
else if(thisTotal < oTotal)
return -1;
else return 1;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.