Consider the following definition of the class MyClass: public class MyClass

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

 

 

Exercise 1

 

Consider the following definition of the class MyClass:

 

public class MyClass

 

{

 

   private static int count = 0;

 

   private int x;

 

   public MyClass(int i)

 

   {

 

      x = i;

 

   }

 

   public void incrementCount()

 

   {

 

      count++;      

 

   }

 

   public void printX()

 

   {

 

      System.out.println("Value of x : " + x);

 

   }

 

   public static void printCount()

 

   {

 

      System.out.println("Value of count : " + count);

 

   }

 

}

 

public class MyClassDemo

 

{

 

   public static void main(String[] args)

 

   {

 

        MyClass myObject1 = new MyClass(5);

 

        MyClass myObject2 = new MyClass(7);        

 

   }

 

}

What is the output of the following Java code? (Assume that following statements are written inside main)

 

myObject1.printX();

 

myObject1.incrementCount();

 

MyClass.incrementCount();

 

myObject1.printCount();

 

myObject2.printCount();

 

myObject2.printX();

 

myObject1.setX(14);

 

myObject1.incrementCount();

 

myObject1.printX();

 

myObject1.printCount();

 

myObject2.printCount();

 

----------------------------

 

Exercise 2 - Give the output of this code

 

public class Circle { // Save as "Circle.java"

 

   // private instance variable, not accessible from outside this class

 

   private double radius;

 

   private String color;

 

  

 

   // Constructors (overloaded)

 

   /** Constructs a Circle instance with default value for radius and color */

 

   public Circle() { // 1st (default) constructor

 

      radius = 1.0;

 

      color = "red";

 

   }

 

   /** Constructs a Circle instance with the given radius and default color */

 

   public Circle(double r) { // 2nd constructor

 

      radius = r;

 

      color = "red";

 

   }

 

   /** Returns the radius */

 

   public double getRadius() {

 

     return radius; 

 

   }

 

   

 

   /** Returns the area of this Circle instance */

 

   public double getArea() {

 

      return radius*radius*Math.PI;

 

   }

 

}

 

-----

 

public class TestCircle { // Save as "TestCircle.java"

 

   public static void main(String[] args) {

 

      Circle c1 = new Circle();

 

       System.out.println("The circle has radius of " 

 

         + c1.getRadius() + " and area of " + c1.getArea());

 

      Circle c2 = new Circle(2.0);

 

      System.out.println("The circle has radius of " 

 

         + c2.getRadius() + " and area of " + c2.getArea());

 

    

 

   }

 

}

 

--------------------------

 

Exercise 3

 

Write a program that would print the information (name, year of joining, address) of three employees by creating a class named 'Employee'. The output should be as follows:

 

Name Year of joining Address

 

Robert 1994 Liloan

 

Sam 2000 Danao

 

John 1999 Cebu City

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Software Development
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education