Question

Write a Java class, named MultProgression, as a subclass of the Progression class, so that: • Each value in the progression i
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

1.Create Progression class with two constructors: one is default constructor, it initializes the first two elements of progression as 1,2

2.Also create a parameterized constructor that can initialize the first two elements of the progression with specified values .

3. create a method testProgression() that constructs remaining values and prints 10 values of the progression

4.create MultiProgression class as a subclass for Progression

5.create the Default constructor in MultiProgression class and call super class constructor using super keyword. Do the same thing for Parameterized constructor

6.create the main method in MultiProgression class and create object for MultiProgression class and call the methods testProgression()

Java Program:

class Progression
{
int x[];
   Progression()
   {
   x=new int[10];
   x[0]=1;
   x[1]=2;
   }
   Progression(int first,int second)
   {
   x=new int[10];
   x[0]=first;
   x[1]=second;
   }

   public void testProgression()
   {
   int i;
   for( i=2;i<=9;i++)
      {
       x[i]=x[i-1]+x[i-2];
       }
   for(i=0;i<10;i++)
       {
       System.out.println(x[i]);
       }
   }
}
class MultiProgression extends Progression
{
   MultiProgression()
   {
   super();
   }

   MultiProgression(int first,int second)
   {
   super(first,second);
   }
  
   public static void main(String args[])
   {
   MultiProgression mp=new MultiProgression(3,5);
   mp.testProgression();
   }
}

OUTPUT:

D:\Java\jdk-10.0.2\bin>javac MultiProgression.java

D:\Java\jdk-10.0.2\bin>java MultiProgression
3
5
8
13
21
34
55
89
144
233

Add a comment
Know the answer?
Add Answer to:
Write a Java class, named MultProgression, as a subclass of the Progression class, so that: •...
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
  • 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...

  • Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following...

    Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: · Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. · A no-arg constructor that creates a default rectangle. · A constructor that creates a...

  • please write in Java and include the two classes Design a class named Fan (Fan.java) to...

    please write in Java and include the two classes Design a class named Fan (Fan.java) to represent a fan. The class contains: An int field named speed that specifies the speed of the fan. A boolean field named fanStatus that specifies whether the fan is on (default false). A double field named radius that specifies the radius of the fan (default 5). A string field named color that specifies the color of the fan (default blue). A no-arg (default) constructor...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class...

    PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class has 4 attributes: (1) student ID: protected, an integer of 10 digits (2) student name: protected (3) student group code: protected, an integer (1 for undergraduates, 2 for graduate students) (4) student major: protected (e.g.: Business, Computer Sciences, Engineering) Class Student declaration provides a default constructor, get-methods and set-methods for the attributes, a public abstract method (displayStudentData()). PART II: Create a Java class named...

  • need help in java , on jgrasp Write a class named Room that has two fields...

    need help in java , on jgrasp Write a class named Room that has two fields one for the room's length and one for the idth The class should have a constructor that sets the length and the width anda method named "getArea" that returns the room's area (area = length x width). should also have a method named "isEqual" (with a Room argument) that determines the two rooms are equal or not. (25 points). Do not write the demo...

  • Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write...

    Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write a set method to make sure that if age > 125 years or less than 0 then it is set to 0 (default value) What does it indicate when declaring a class's instance variables with private access modifier? What is the role of a constructor? The data part of by an object's instance variables is known as? Do all methods have to always return...

  • Java code please. Please help * 95: Write a public class named TollInts with the following....

    Java code please. Please help * 95: Write a public class named TollInts with the following. * -A public constructor that takes 2 doubles as inputs. * -A public method named compute that takes no parameters and returns the the tangent of the * first constructor input subtracted by the the square root of the second constructor input * as a double. You will need to store the constructor input in instance variables to be able * to access them...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

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
ADVERTISEMENT