// CS 2510 Spring 2011 // Lab 4: Files.java import tester.*; // Represents various kinds of files interface IFile{ // Compute the size of this file in bytes int size(); } // Represents a text file class TextFile implements IFile{ String name; String owner; int length; // in bytes TextFile(String name, String owner, int length){ this.name = name; this.owner = owner; this.length = length; } // Compute the size of this TextFile, in bytes int size(){ return this.length; } } //to represent an image file class ImageFile implements IFile{ String name; String owner; int width; // in pixels int height; // in pixels ImageFile(String name, String owner, int width, int height){ this.name = name; this.owner = owner; this.width = width; this.height = height; } // Compute the size of this ImageFile, in bytes int size(){ return this.width * this.height; } } //to represent an audio file class AudioFile implements IFile{ String name; String owner; int speed; // in bytes per second int length; // in seconds of recording time AudioFile(String name, String owner, int speed, int length){ this.name = name; this.owner = owner; this.speed = speed; this.length = length; } // compute the size of this file int size(){ return this.speed * this.length; } } // Examples class for Files class FileExamples{ FileExamples(){} IFile text = new TextFile("Files.java", "vkp", 2048); IFile img = new ImageFile("photo.jpg", "chadwick", 640, 480); IFile aud = new AudioFile("watcha_want.mp3", "shivers", 44, 120000); // Test the size method boolean testSize(Tester t){ return (t.checkExpect(this.text.size(), 2048) && t.checkExpect(this.img.size(), 307200) && t.checkExpect(this.aud.size(), 5280000)); } }