Question

In: Computer Science

How to write a Java program that has a base class with methods and attributes, subclasses...

How to write a Java program that has a base class with methods and attributes, subclasses with methods and attributes. Please use any example you'd like.

Solutions

Expert Solution

CODE:

class Demo2{
   int a=10,b=40;
   void add(){
       System.out.println("Sum of a and b values is "+(a+b)+ "from Demo2");
   }
   void mul(){
       System.out.println("Multiplication of a and b values is "+(a*b)+ "from Demo2");
   }
}

class Demo extends Demo2{
   int a=12,b=20;
   void print()
   {
       System.out.println("Sum of a and b values is"+(a+b));
   }
   void multi(){
       System.out.println("Multiplication of a and b values is "+(a*b));
   }
   public static void main(String[] args) {
       Demo d=new Demo();
       d.print();
       d.multi();
       d.add();
       d.mul();

   }
}

OUTPUT:

If you have any doubts please COMMENT....

If you understand the answer please give THUMBS UP....


Related Solutions

Write a java program StudentDriver class to test the methods of the Student class. Use data:...
Write a java program StudentDriver class to test the methods of the Student class. Use data: Mary Brown 1234 John Jones 5678 Maty Jones 1234 Note: we are including Mary Jones twice so we can test the "equals" method. We can test "compareTo", by test student1 and student2, then student2 and student3. Just submit your driver class as text file. Make sure you also test addQuiz, getTotalScore, getAverage, and toString. Don't worry about testing all of the sets and gets......
Language: Java To be able to code a class structure with appropriate attributes and methods. To...
Language: Java To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure to...
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
IN JAVA!   Write a class Store which includes the attributes: store name, city. Write another class...
IN JAVA!   Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...
Write in Java the following: A.  A bank account class that has three protected attributes: account number...
Write in Java the following: A.  A bank account class that has three protected attributes: account number (string), customer name (string), and balance (float). The class also has a parameterized constructor and a method public void withDraw (float amount) which subtracts the amount provided as a parameter from the balance. B. A saving account class that is a child class of the bank account class with a private attribute: penality rate (float). This class also has a parameterized constructor and a...
Please write a java program that has the following methods in it: (preferably in order)   a...
Please write a java program that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled, and the total tuition Design Notes: The...
This program is written in Java and should be modularized in methods in one class. This...
This program is written in Java and should be modularized in methods in one class. This program will calculate the Gross Earnings, FICA tax (Medicare and Social Security taxes), Federal Tax Withheld, and Net Amount of the payroll check for each employee of a company. The output must contain numbers with 2 decimal places. The user input must be validated – if incorrect data is entered, send an error message to the user. INPUT The application must be able to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT