Question

In: Computer Science

Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...

Class Exercise: Constructor using JAVA

Let’s define a Class together and have a constructor while at it.

- What should the Class object represent? (What is the “real life object” to represent)?

- What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor)

- What should happen when a new instance of the Class is created?

- Question: What are you allowed to do in the constructor?

- Let’s test this out (demo).

Solutions

Expert Solution

The Class in any Object Oriented Programming Language represents a template or a blue print.

So an object of that class is an instance of a class.

Class has just logical existence but an object has physical existence.

When an object is created, the space required for the object is allocated. Any constructors that class has are executed on the object. The constructor should have the same name as the class.

We can initialise the variables, allocate memory.

Please find below the demo program.

import java.util.*;
import java.lang.*;
import java.io.*;


class sample
{
   int demo;
  
   sample()
   {
       demo=100;   //the variable is initialised using the constructor.
   }
  
   void display()
   {
       System.out.println("Demo value is "+demo);
   }
   }
class Demo_class
{
   public static void main (String[] args) throws java.lang.Exception
   {

       sample s=new sample();   //a new object is created.
      
       s.display();
      
   }
}


Related Solutions

JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor //...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor // Destructors ? If none, explain why double getX(); double getY(); // Overload Compare operators <, >, ==, >=, <=, points are compare using their distance to the origin: i.e. SQR(X^2+Y^2) private: double x, y; };
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
This is a JAVA assignment and i dont have the SinglyLinkedList class   Exercise 1 In this...
This is a JAVA assignment and i dont have the SinglyLinkedList class   Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list. Exercise 2...
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp....
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp. Constructor checks if the temperature is between 0 and 500. If the temperature does not fit the requirement, then set the temperature to be zero. Otherwise, set it to be the value passed to the constructor. Set the power to be off. Set the String to be the color passed to the constructor. Use the same requirement for the constructor for the set temperature...
Java program Create a constructor for a class named Signal that will hold digitized acceleration data....
Java program Create a constructor for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp data The constructor...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create an application with 5 classes: 1.) Vehicle 2.) Car 3.) Bus 4.) Truck 5.) Tester Following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassengers, isCpnvertable, numberSeats, maxWeightLoad, numberAxels **Class Vehicle: should have a constructor that initializes all of it's data **Classes Car, Bus, Truck: will have constructors that resuse their parents constructors and provide additional code for initalizing their specific data...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT