Question

Explain why certain statements don't compile that mostly related to extends and implements when u...

Explain why certain statements don't compile that
mostly related to extends and implements when
used with abstract and final classes.

Interpret two different recusive methods.

Interpret a multi-class program that uses
the main() method's String[] parameter.

Interpret a multi-class program that includes
an interface and uses polymorphism.

Interpret a code snippet that uses a String[][].

Interpret a single-class program that assesses
understanding of the inherited toString() and
equals() methods.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.Abstract methods compulsory we should override in child classes to provide implementation.

Where as we can't override final methods. Here final, abstract combination is illegal combination for methods.

NOTE :- A final class can not have abstract methods and an abstract class can not be declared final.

2.Recursive Methods:

1.Factorial of a number.

public class Fact {  

    static int factorial(int n){      

          if (n == 1)      

            return 1;      

          else      

            return(n * factorial(n-1));      

    }      

  

public static void main(String[] args) {  

System.out.println("Factorial of 5 is: "+factorial(5));  

}

}

output:

Factorial of 5 is: 120

prints the factorial of 5.

2.Fibonacci Series

public class Fib{  

    static int n1=0,n2=1,n3=0;      

     static void printFibo(int count){      

        if(count>0){      

             n3 = n1 + n2;      

             n1 = n2;      

             n2 = n3;      

             System.out.print(" "+n3);     

             printFibo(count-1);      

         }      

     }        

  

public static void main(String[] args) {  

    int count=15;      

      System.out.print(n1+" "+n2);

      printFibo(count-2);

}  

}

output:

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

prints the first 15 numbers in Fibonacci series.

3.Interpret string[] in main() method.

we have to take the command line argument.

class cmd
{
  public static void main(String[] args)
  {
    for(int i=0;i< args.length;i++)
    {
    System.out.println(args[i]);
    }
  }
}

output:

Command Prompt F:\〉javac cnd-Java F:\〉java cmd 10 20 20 30 30

4.Multi class program that includes interface and uses polymorphism.

interface Anim
{
public void eat();
}
class Animal implements Anim{
public void eat()
{
System.out.println("animal is eating...");
}
}
class Dog extends Animal{
public void eat(){
System.out.println("dog is eating...");
}
}
class Main extends Dog{
public static void main(String args[]){
Animal a;
a=new Animal();
a.eat();
a=new Dog();
a.eat();
}
}

output:

animal is eating. s eating- - -

5.code that uses string[][].

import java.io.*;

class GFG {

    public static void print2D(int mat[][])

    {

        // Loop through all rows

        for (int i = 0; i < mat.length; i++)

  

            // Loop through all elements of current row

            for (int j = 0; j < mat[i].length; j++)

                System.out.print(mat[i][j] + " ");

    }

  

    public static void main(String args[]) throws IOException

    {

        int mat[][] = { { 1, 2, 3, 4 },

                        { 5, 6, 7, 8 },

                        { 9, 10, 11, 12 } };

        print2D(mat);

    }

}

output:

1 2 3 4 5 6 7 8 9 10 11 12

prints all elements in the two dimensional matrix.

6.

Interpret a single-class program that assesses understanding of the inherited toString() and
equals() methods

code:

class Main
{
//the class defining a circle
int radius;
public Main(int i)
{
radius = i;
}

//Overriding toString() method

@Override
public String toString()
{
return "Area = "+(3.14*radius*radius);
}
//if both have same radius
public void equals(Main b)
{
if(this.radius==b.radius)
System.out.print("Both circles are equal.");
else
System.out.print("Circles are not");
}
  
public static void main(String[] args)
{
Main a = new Main(10); // Creating an instance of A
Main b=new Main(10);
System.out.println(a);
a.equals(b);
}
}

output:

Area314.0 Both circles are equal.

If you have any queries, please comment below.

Please upvote , if you like this answer.

Add a comment
Know the answer?
Add Answer to:
Explain why certain statements don't compile that mostly related to extends and implements when u...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Copy the program AmusementRide.java to your computer and add your own class that extends class AmusementRide....

    Copy the program AmusementRide.java to your computer and add your own class that extends class AmusementRide. Note: AmusementRide.java contains two classes (class FerrisWheel and class RollerCoaster) that provide examples for the class you must create. Your class must include the following. Implementations for all of the abstract methods defined in abstract class AmusementRide. At least one static class variable and at least one instance variable that are not defined in abstract class AmusementRide. Override the inherited repair() method following the...

  • IN C# Objectives: Create an application that uses a dictionary collection to store information ...

    IN C# Objectives: Create an application that uses a dictionary collection to store information about an object. Understanding of abstract classes and how to use them Utilize override with an abstract class Understanding of Interfaces and how to use them Implement an Interface to create a “contract” between classes. Compare and contrast inheritance and interfaces. Instructions: Interface: Create an interface called ITrainable which contains the following: Dictionary<string, string> Behaviors{ get; set; } string Perform(String signal); string Train(String signal, string behavior);...

  • I need some help i need to do this in C# Objectives: • Create an application...

    I need some help i need to do this in C# Objectives: • Create an application that uses a dictionary collection to store information about an object. • Understanding of abstract classes and how to use them • Utilize override with an abstract class • Understanding of Interfaces and how to use them • Implement an Interface to create a “contract” between classes. • Compare and contrast inheritance and interfaces. Instructions: Interface: Create an interface called ITrainable which contains the...

  • Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume...

    Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume of rectangular objects. First, write an abstract super class RectangularShape, then write a subclass Rectangle that inherits from the super class to compute areas of rectangles, including squares if there is only one data. Finally, write another subclass Cuboid that inherits from its super class Rectangle above to compute surface areas and volumes of cuboids, including 3 equal sided cube. Must apply code-reusability in...

  • Add an HourlyPlusCommissionEmployee class to the PayrollSystem app by subclassing an existing class. A HourlyPlusCommissionEmployee is...

    Add an HourlyPlusCommissionEmployee class to the PayrollSystem app by subclassing an existing class. A HourlyPlusCommissionEmployee is a kind of CommissionEmployee with the following differences and specifications: HourlyPlusCommissionEmployee earns money based on 2 separate calculations: commissions are calculated by the CommissionEmployee base class hourly pay is calculated exactly the same as the HourlyEmployee class, but this is not inherited, it must be duplicated in the added class BasePlusCommissionEmployee inherits from CommissionEmployee and includes (duplicates) the details of SalariedEmployee. Use this as...

  • In Java Which of the following statements declares Salaried as a subclass of payType? Public class...

    In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...

  • //*Manager.java*// public interface Manager { public void handleCrisis(); } _________________________________________________________ /**Employee.java**/ Public abstract class Employee {...

    //*Manager.java*// public interface Manager { public void handleCrisis(); } _________________________________________________________ /**Employee.java**/ Public abstract class Employee { protected final String name; protected final int id; public Employee(String empName, int empId) { name = empName; id = empId; } public Employee() { name = generatedNames[lastGeneratedId]; id = lastGeneratedId++; } public String getName() { return name; } public int getId() { return id; } //returns true if work was successful. otherwise returns false (crisis) public abstract boolean work(); public String toString() { return...

  • 10.3 Example. When you first declare a new list, it is empty and its length is...

    10.3 Example. When you first declare a new list, it is empty and its length is zero. If you add three objects—a, b, and c—one at a time and in the order given, to the end of the list, the list will appear as a b c The object a is first, at position 1, b is at position 2, and c is last at position 3.1 To save space here, we will sometimes write a list’s contents on one...

  • The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and...

    The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a...

  • CARD GAME (LINKING AND SORTING) DATA STRUCTURES TOPIC(S) Topic Primary Linked lists Sorting Searching Iterators As...

    CARD GAME (LINKING AND SORTING) DATA STRUCTURES TOPIC(S) Topic Primary Linked lists Sorting Searching Iterators As needed Recursion OBJECTIVES Understand linked lists and sorting concepts and the syntax specific to implementing those concepts in Java. PROJECT The final objective of this project is to create a multi-player card game, but you will create your classes in the order given, as specified. (Analogy: you must have a solid foundation before you can build a house). In order to allow creative freedom,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT