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();
}
}