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...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
using java Define a class Library based on the following specifications: a. A library can have...
using java Define a class Library based on the following specifications: a. A library can have multiple books. Decide on the best possible data structure to store books. b. A library has a name. c. A library has an address. Define a 2-argument constructor for the Library class. Decide on the arguments needed for this constructor. e. Define a method that can add new books to the library. f. Define a method that allows a book to be borrowed (checked...
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.
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...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
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; };
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT