Question

In: Computer Science

1. define a class that has three different constructors. 2. write code that overrides the following...

1. define a class that has three different constructors.

2.

write code that overrides the following method

public double sqr(double num1) {

return num1*num1;

}

Solutions

Expert Solution

public class Num {
   private double number;

   // default constructor
   Num() {
       number = 0;
   }

   // parameterized constructor
   Num(double i) {
       number = i;
   }

   // copy constructor
   Num(Num n) {
       this.number = n.getNumber();
   }

   public double getNumber() {
       return number;
   }

   public void setNumber(double aNumber) {
       number = aNumber;
   }

   public double sqr(double num1) {

       return num1 * num1;

   }

   public static void main(String[] args) {
       Num n = new Num(5);
       System.out.println(n.sqr(10));
   }
}


Related Solutions

Code a simple Editor class with 2 constructors (one default, one with a string for a...
Code a simple Editor class with 2 constructors (one default, one with a string for a file name) The Editor class in private has an instance of a LinkedList (of strings) and the position of the user (use our Point class) create a short file - at least 5 lines - could be a program main: string fileName("input.txt"); Editor miniVi (fileName); miniVi.displayLines(); C++ explain in detail plz Thank you
Write code to define a function named mymath. The function has three arguments in the following...
Write code to define a function named mymath. The function has three arguments in the following order: Boolean, Integer, and Integer. It returns an Integer. The function will return a value as follows: 1. If the Boolean variable is True, the function returns the sum of the two integers. 2. If the Boolean is False, then the function returns the value of the first integer - the value of the second Integer.
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash()...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash() method to indicate that pants are dry clean only. Include additional method hang() Add to your driver/tester file to make and print new pants objects and test it. Shirt class Include additional property of type string called sleeves. Write necessary constructors For sleeves only allow it to be set to {"short", "long", "none"} For size, only allow {"S","M","L"} Override the wash() method to indicate...
Create a class called College that has: At least 2 constructors (one should be the default...
Create a class called College that has: At least 2 constructors (one should be the default constructor) All members private The relevant setters and getters A print_me() function A college_ID member (unique) Write two unsorted lists that manage the data for colleges and their ranking: An unsorted list using an array An unsorted list using a linked list Each college will have a unique ID number that will be used for search. For the lab you don't need to read...
Define the exception class called TornadoException. The class should have two constructors including one default constructor....
Define the exception class called TornadoException. The class should have two constructors including one default constructor. If the exception is thrown with the default constructor, the method getMessage should return "Tornado: Take cover immediately!" The other constructor has a single parameter, m, of int type. If the exception is thrown with this constructor, the getMessage should return "Tornado: m miles away; and approaching!" Write a Java program to test the class TornadoException.
C++: 1.When and in what order are constructors and destructors called? 2. Create a class without...
C++: 1.When and in what order are constructors and destructors called? 2. Create a class without any constructors, and show that you can create objects with the default constructor. Now create a non-default constructor (one with an argument, aka parameterized constructor) for the class, and try compiling again. Explain what happened.
Write a Java class called Employee (Parts of the code is given below), which has three...
Write a Java class called Employee (Parts of the code is given below), which has three private fields firstName (String), lastName (String) and yearsEmployed (double). Implement accessor/mutator methods for the private fields and toString() method, that returns a String representation, which contains employee name and years she was employed. public class Employee { private String firstName; ... } Write a client program called EmployeeClient that creates an instance of Employee class, sets values for the fields (name: John Doe, yearsEmployed:...
Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container...
Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container Classes.
Using Java, define a train class and write code to demonstrate its functionality The train should...
Using Java, define a train class and write code to demonstrate its functionality The train should run between Boston and New York, stopping at different, different station along the way. Provide output to indicate the train’s current status.
1. (a) Define social class. How is social class different for the other systems of stratification...
1. (a) Define social class. How is social class different for the other systems of stratification such as slavery and caste? (b) There are three sociological perspectives on stratification. Structural Functionalism (Davis and Moore 1945); Conflict Theory (Marx and Engels); Max Weber’s three-part theory—define each of the elements. Briefly summarize each of these theories.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT