Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming. Write one application program by using the following requireme

ID: 3692564 • Letter: J

Question

Java Programming. Write one application program by using the following requirements:

1. File input and output

2. Exception handling

3. You can create your own exception classes or you can using existing exception classes from Java API

4. Inheritance

5. At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods.

6. At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods.

7. At least one interface: this interface needs to have at least two abstract methods.

8. At least one method overloading.

9. At least one method overriding.

10. At least one static member and one static method.

11. Polymorphism: you need to have at least one superclass reference to reference objects of a subclass.

12. At least one aggregation in a class.

Please include comments at the place where you use the above items. Thank you!

Explanation / Answer

9.At least one method overriding

class Animal{

   public void move(){
      System.out.println("Animals can move");
   }
}

class Dog extends Animal{

   public void move(){
      System.out.println("Dogs can walk and run");
   }
}
class TestDog{

   public static void main(String args[]){
      Animal a = new Animal(); // Animal reference and object
      Animal b = new Dog(); // Animal reference but Dog object

      a.move();// runs the method in Animal class

      b.move();//Runs the method in Dog class
   }
}

8.At least one method overloading

code:

class   Test
{

   int sum(int x,int y)
   {
       return x+y;
   }
int sum(int x,int y,int z)
   {
       return x+y+z;
   }
float sum(int x,float y)
   {
       return x+y;
   }
float sum(float x,int y)
   {
       return x+y;
   }
float sum(float x,float y)
   {
       return x+y;
   }
}//class

class OverLoadTest
{

   public static void main(String[] args)
   {
       Test t=new Test();
       int res=t.sum(34,56);
       System.out.println("sum is"+res);
       System.out.println("sum is"+t.sum(45,23.56f));
       System.out.println("sum is"+t.sum(45,89));
       System.out.println("sum is"+t.sum(45.45f,67));
       System.out.println("sum is"+t.sum(45.34f,23.56f));
       System.out.println("sum is"+t.sum(34,56,78));
   }//main
}//class

10:At least one static member and one static method.

code:

public class swappingExample {

   public static void main(String[] args) {
      int a = 30;
      int b = 45;

      System.out.println("Before swapping, a = " +
                          a + " and b = " + b);

      // Invoke the swap method
      swapFunction(a, b);
      System.out.println(" **Now, Before and After swapping values will be same here**:");
      System.out.println("After swapping, a = " +
                         a + " and b is " + b);
   }

   public static void swapFunction(int a, int b) {

      System.out.println("Before swapping(Inside), a = " + a
                           + " b = " + b);
      // Swap n1 with n2
      int c = a;
      a = b;
      b = c;

      System.out.println("After swapping(Inside), a = " + a
                           + " b = " + b);
   }
}

11. Polymorphism: you need to have at least one superclass reference to reference objects of a subclass.

code:

/* File name : Employee.java */
class Employee
{
   private String name;
   private String address;
   private int number;
   public Employee(String name, String address, int number)
   {
      System.out.println("Constructing an Employee");
      this.name = name;
      this.address = address;
      this.number = number;
   }
   public void mailCheck()
   {
      System.out.println("Mailing a check to " + this.name
       + " " + this.address);
   }
   public String toString()
   {
      return name + " " + address + " " + number;
   }
   public String getName()
   {
      return name;
   }
   public String getAddress()
   {
      return address;
   }
   public void setAddress(String newAddress)
   {
      address = newAddress;
   }
   public int getNumber()
   {
     return number;
   }
}

/* File name : Salary.java */
class Salary extends Employee
{
   private double salary; //Annual salary
   public Salary(String name, String address, int number, double
      salary)
   {
       super(name, address, number);
       setSalary(salary);
   }
   public void mailCheck()
   {
       System.out.println("Within mailCheck of Salary class ");
       System.out.println("Mailing check to " + getName()
       + " with salary " + salary);
   }
   public double getSalary()
   {
       return salary;
   }
   public void setSalary(double newSalary)
   {
       if(newSalary >= 0.0)
       {
          salary = newSalary;
       }
   }
   public double computePay()
   {
      System.out.println("Computing salary pay for " + getName());
      return salary/52;
   }
}
/* File name : VirtualDemo.java */
class VirtualDemo
{
   public static void main(String [] args)
   {
      Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
      Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
      System.out.println("Call mailCheck using Salary reference --");
      s.mailCheck();
      System.out.println(" Call mailCheck using Employee reference--");
      e.mailCheck();
    }
}

12:At least one aggregation in a class

code:

class Author
{
String authorName;
int age;
String place;
Author(String name,int age,String place)
{
this.authorName=name;
this.age=age;
this.place=place;
}
public String getAuthorName()
{
return authorName;
}
public int getAge()
{
return age;
}
public String getPlace()
{
return place;
}
}

class Book
{
String name;
int price;
Author auth;
Book(String n,int p,Author at)
{
this.name=n;
this.price=p;
this.auth=at;
}
public void showDetail()
{
System.out.println("Book is"+name);
System.out.println("price "+price);
System.out.println("Author is "+auth.getAuthorName());
}
}

class Test
{
public static void main(String args[])
{
Author ath=new Author("Me",22,"India");
Book b=new Book("Java",550,ath);
b.showDetail();
}
}

6. At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods.

code:

class Super_class{

   int num = 20;

   //display method of superclass
   public void display(){
      System.out.println("This is the display method of superclass");
   }  

}

class Sub_class extends Super_class {

   int num = 10;

   //display method of sub class
   public void display(){
      System.out.println("This is the display method of subclass");
   }

   public void my_method(){
  
      //Instantiating subclass
      Sub_class sub = new Sub_class();
  
      //Invoking the display() method of sub class
      sub.display();
  
      //Invoking the display() method of superclass
      super.display();
  
      //printing the value of variable num of subclass
      System.out.println("value of the variable named num in sub class:"+ sub.num);
      
      //printing the value of variable num of superclass
      System.out.println("value of the variable named num in super class:"+ super.num);   
   }

   public static void main(String args[]){
      Sub_class obj = new Sub_class();
      obj.my_method();
    
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote