Write ONE application program (ONE MAIN METHOD) by using the following requirements:
File input and output (10 points)
Exception handling (10 points)
You can create your own exception classes or
You can use existing exception classes from Java API
Inheritance
At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods (10 points)
At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods (10 points)
At least one interface: this interface needs to have at least two abstract methods (10 points)
At least one method overloading (10 points)
At least one method overriding (10 points)
At least one static member and one static method (10 points)
Polymorphism: you need to have at least one superclass reference to reference objects of a subclass (10 points)
At least one aggregation in a class (10 points)
Within your project, PLEASE include comments at the place where you use the above items. I will deduct the points if I cannot find the above items within your project.
//use of polymorphism and method overloading.
//animalSound method is used in different ways thus polymorphism.
//have used static methods here
class Animal {
public static String animalSound() { //static method in super class
return "This is animal sound:";
}
}
class Donkey extends Animal {
public static String animalSound(String s) { //static method in sub-class Donkey
return s;
}
}
class Dog extends Animal {
public static String animalSound(String f) { // static method in Dog class
return f;
}
}
//use File I/O
class FileIO {
public String writeToFile(String s) {
//file output, writing to a file
return s;
}
//file input, reading a file
public String readFile(String contents) {
return contents;
}
}
public class MyProgram {
public static void main(String args[]) throws IOException {
//create object of class MyInteger and try to access members of super class Numbers
System.out.println("Sample output for inheritance class:");
MyInteger num1 = new MyInteger();
Scanner int1 = new Scanner(System.in);
System.out.println("Enter a Number:");
Integer num = int1.nextInt();
num1.setInteger(num);
System.out.println("You entered:" + num1.getInteger());
//test aggregation output
System.out.println("Printing area of circle in aggregation:");
Circle c = new Circle();
Scanner radius = new Scanner(System.in);
System.out.println("Enter Circle radius:");
Double d = radius.nextDouble();
System.out.println("Area is:" + c.area(d));
//test polymorphism
System.out.println("Printing animal sounds using polymorphism:");
Donkey dh = new Donkey();
Dog dg = new Dog();
Animal a = new Animal();
System.out.println("What sound is produced by a Dog? Enter sound Below:");
Scanner sound = new Scanner(System.in);
String sd = sound.nextLine();
System.out.println(a.animalSound());
System.out.println("A Dog:" + dg.animalSound(sd));
System.out.println("What sound is produced by a Donkey? Enter sound Below:");
Scanner sound2 = new Scanner(System.in);
String sd2 = sound2.nextLine();
System.out.println(a.animalSound());
System.out.println("A Donkey:" + dh.animalSound(sd2));
// test File input/output
System.out.println("This is File I/O:");
FileIO file = new FileIO();
Scanner file1 = new Scanner(System.in);
System.out.println("Enter name of file to write: e.g myfile.txt");
String filename = file1.nextLine();
FileOutputStream fout;
try {
fout = new FileOutputStream(file.writeToFile(filename));
Scanner filecontent = new Scanner(System.in);
System.out.println("Enter what you want to write in the file:");
String s = filecontent.nextLine();
byte b[] = s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("success...");
} catch (Exception e) {
System.out.println(e);
}
//read file created
FileInputStream fin = null;
try {
fin = new FileInputStream(filename);
} catch (FileNotFoundException ex) {
Logger.getLogger(MyProgram.class.getName()).log(Level.SEVERE, null, ex);
}
int i = 0;
System.out.println("File content is:");
while ((i = fin.read()) != -1) {
System.out.print((char) i);
}
System.out.println();
fin.close();
}
}
Share To Friends Via:
More Questions For Programming in Java:
What is Java? (Answered) What is Java? . Posted On:Sat 20, March 2021 20:03:42 pm