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.


Answer:


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

//inheritance.
//below is my super class.
class Numbers {

//class Numbers data members
protected Integer myint;
protected Double mydouble;

// Numbers class mutators
public void setInteger(Integer i) {

this.myint = i;
i.toString(); // use of toString Method

}

public void setDouble(Double j) {
this.mydouble = j;
j.toString(); //use of toString method
}
// Numbers class accessors

public Integer getInteger() {
return myint;
}

public Double getDouble() {
return mydouble;
}
}
//MyInteger class which is sub-class of Numbers
class MyInteger extends Numbers {
//data members

protected Integer a;
//mutator method

public void setMyint(Integer b) {
this.a = b;
a.toString(); // toString Method
}
//Accessor method

public Integer getMyint() {
return a;
}
}
//Double class which is sub-class of Numbers
class MyDouble extends Numbers {
//data members

protected Double c;
//mutator method

public void setMydouble(Double d) {
this.c = d;
c.toString(); // toString Method
}
//Accessor method

public Double getMydouble() {
return c;
}
}

//use of interfaces
interface Insect {

//abstract methods of insect interface
public void insectMovement(); // abstract method of inteface has no body

public void insectInhabitat();

}
//a class to implement our interface

class Grasshopper implements Insect {
//method overriding

static String s = "Jumps"; // static member
static String i = "Thickets"; //static member

@Override
public void insectMovement() {
System.out.println(s); //standard output
}
//method overriding

@Override
public void insectInhabitat() {
System.out.println(i); //standard output
}

}

//use of aggregation in a class
class Operate {

double square(double m) {
return m * m;
}
}

class Circle {

Operate op;//class aggregation
double db;

double area(double radius) {

//Exception handling using try catch block.
try {
op = new Operate();
db = op.square(radius);

} catch (Exception e) {
e.printStackTrace();
}
return Math.PI * db;
}
}

//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
  What are the main features of java? (Answered)
What are the main features of java? .
Posted On:Sat 20, March 2021 20:05:08 pm
  What are pass by reference and passby value? (Answered)
What are pass by reference and passby value? .
Posted On:Sat 20, March 2021 20:06:02 pm
  What is the Java API? (Answered)
What is the Java API? .
Posted On:Sat 20, March 2021 20:06:40 pm
  What is the Java Virtual Machine (JVM)? (Answered)
What is the Java Virtual Machine (JVM)? .
Posted On:Sat 20, March 2021 20:07:19 pm
  What is variables and their types? (Answered)
What is variables and their types? .
Posted On:Sat 20, March 2021 20:08:37 pm
  what is dot operator? (Answered)
what is dot operator? .
Posted On:Sat 20, March 2021 20:09:39 pm
  Define strings? (Answered)
Define strings? .
Posted On:Sat 20, March 2021 20:10:13 pm
  What is serialization? (Answered)
What is serialization? .
Posted On:Sat 20, March 2021 20:10:48 pm
  What are different types of access modifiers? (Answered)
What are different types of access modifiers? .
Posted On:Sat 20, March 2021 20:11:26 pm
  What is an abstract class? (Answered)
What is an abstract class? .
Posted On:Sat 20, March 2021 20:12:24 pm
  What are class variables (Answered)
What are class variables .
Posted On:Sat 20, March 2021 20:13:27 pm
  What is the Collection interface? (Answered)
What is the Collection interface? .
Posted On:Sat 20, March 2021 20:14:12 pm
  What must a class do to implement an interface? (Answered)
What must a class do to implement an interface? .
Posted On:Sat 20, March 2021 20:14:51 pm
  What is the Collections API? (Answered)
What is the Collections API? .
Posted On:Sat 20, March 2021 20:15:28 pm
  What is an array? (Answered)
What is an array? .
Posted On:Sat 20, March 2021 20:16:21 pm
  What is a list iterator? (Answered)
What is a list iterator? .
Posted On:Sat 20, March 2021 20:17:11 pm
  What is the main difference between a String and a StringBuffer class? (Answered)
What is the main difference between a String and a StringBuffer class? .
Posted On:Sat 20, March 2021 20:17:58 pm
  When do we use java serialization? (Answered)
When do we use java serialization? .
Posted On:Sat 20, March 2021 20:19:45 pm
  What is the main difference between shallow cloning and deep cloning of objects? (Answered)
What is the main difference between shallow cloning and deep cloning of objects? .
Posted On:Sat 20, March 2021 20:21:47 pm
  What are wrapper classes? (Answered)
What are wrapper classes? .
Posted On:Sat 20, March 2021 20:22:49 pm
  What is the difference between an instance variable and a static variable? (Answered)
What is the difference between an instance variable and a static variable? .
Posted On:Sat 20, March 2021 20:23:45 pm
  What is type casting? (Answered)
What is type casting? .
Posted On:Sat 20, March 2021 20:26:16 pm
  What is a user defined exception? (Answered)
What is a user defined exception? .
Posted On:Sat 20, March 2021 20:26:58 pm
  What is an instanceof operator? (Answered)
What is an instanceof operator? .
Posted On:Sat 20, March 2021 20:27:54 pm
  What are runtime exceptions? (Answered)
What are runtime exceptions? .
Posted On:Sat 20, March 2021 20:28:47 pm
  What is the difference between an interface and an abstract class? (Answered)
What is the difference between an interface and an abstract class? .
Posted On:Sat 20, March 2021 20:30:26 pm
  what is a package? (Answered)
what is a package? .
Posted On:Sat 20, March 2021 20:31:20 pm
  Why do threads block on I/O? (Answered)
Why do threads block on I/O? .
Posted On:Sat 20, March 2021 20:32:16 pm
  What is the List interface? (Answered)
What is the List interface? .
Posted On:Sat 20, March 2021 20:33:41 pm

More Questions Categories:


About Us

Contact us

Terms of use | Privacy policy

Follow Us:               

All Rights Reserved © 2024; pscustudies.com