Greetings !
Note: 1. in a single java file we can have multiple classes but only one of them can be public. so i have removed public keyword from class Office.
Note 2: copy the following source code and save it as a Main.java file, thereafter compile and run it in java editor.
// Source Java Code
public class Main{
public static void main(String []args){
Office office = new Office("5421 sennott square");
office.setOccupant("luis");
office.width = 5.0;
office.setDepth(4.0);
office.closeDoor();
System.out.println(office.toString());
}
}
class Office{
// declare width variable as a type double, which is set by main
method to 5.
double width;
/* When we call the constructor Office(), we pass a parameter to
the constructor ("5421 sennott square"),
which will set the value of Location to "5421 sennott square":
*/
Office(String Location)
{
System.out.println("office Information :"); // print office
info
System.out.println("Location: "+ Location); // print location
}
/* When we call the method setOccupant(), we pass a parameter to
the method ("luis"),
which will set the value of Occupant to "luis": */
void setOccupant(String Occupant)
{
System.out.println("Occupant: "+ Occupant); //print Occupant
}
/* When we call the method setDepth(), we pass a parameter to the
method (4.0), which will set the value of depth to 5,
also we print a area of office by multiplying width to depth
*/
void setDepth(Double depth)
{
System.out.println("Area: "+ width*depth); // it will print
5*4=20
}
//this method simply prints "door: closed"
void closeDoor()
{
System.out.println("Door: Closed");
}
//overriding toString() method, otherwise it will print the
hashCode of the object
public String toString(){
return "";
}
}
//Screenshot of the source java code
Screenshot of the code with output:
Q19 Consider the following main function: 10 Points public class Main { public static void main(String[]...
What output is produced by the following program? public class MysteryNums public static void main(String[] args) - int x = 12; int y = x - 3; 3, sentence (y, x + y); . public static void sentence (int numi, int num2) { 4: System.out.println(num1 + " + num2);
import java.util.Scanner; public class StudentClient { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("Smith", "123-45-6789", 3.2); Student s3 = new Student("Jones", "987-65-4321", 3.7); System.out.println("The name of student #1 is "); System.out.println("The social security number of student #1 is " + s1.toString()); System.out.println("Student #2 is " + s2); System.out.println("the name of student #3 is " + s3.getName()); System.out.println("The social security number...
2016 points output of following Java Program? public class Base { public final void show() { . System.out.println("Base::show() called"); public class Derived extends Base { public void show() { System.out.println("Derived::show() called"); } e lor rested in order public class Main { ects from and public static void main(String[] args) { Base b = new Derived(); b.show();
What is the output of following program: public class Test{ public static void main(String[] args) { A a = new A(): a method B(): } } class A{ public A(){ System out println("A's constructor is executed"): } public void method(){ System out printin ("methodA is executed"): } public void methodAB(){ System out printin ("As methodAB is executed"): } } class B extends A { private int num = 0: public B (){ super(): System out printin ("B's constructor is executed"):...
public class SquareTest { public static void main(String[] args) { System.out.println("Number of sides is " + Square.NUM_OF_SIDES); Square s1 = new Square(-5.7f); System.out.println(s1); s1.setLength(0.001f); System.out.println(s1.getLength()); s1.setLength(-9999); System.out.println(s1.getLength()); System.out.println("Number of sides is " + s1.NUM_OF_SIDES); s1.calculateArea(); } }...
class Test public static void main(String args) { Aa=new AO: a.printo: class A private String s; public A (String news) { 8 = news: public void print { System.out.println(s): The program would compile and run if you change A a new Alto Aa=new A('5'). The program has a compilation error because the instance variables in class A is not public. The program has a compilation error because class A does not have a no-arguments constructor The program compiles and runs...
Review the following code: public class Looping { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { System.out.println(i + " x " + j + " = " + (i * j)); } } } } What is the output from the code above? Replace this text with your solution What happens if you change the...
Consider the following codes: public class TestThread extends Thread { public static void main(String[] args) { TestThread thread = new TestThread(); } @Override public void run() { printMyName(); } private void printMyName() { System.out.println("Thread is running"); } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...
Revision Question Consider the following Java class: { public static void main (String [ ] args) { ArrayQueue<Integer> queue; queue = new ArrayQueue<Integer> () ; Integer x, y ; x = 3; y = 6; queue.offer (x) ; queue.offer (12) ; queue.offer (y) ; y = queue.peek () ; queue.poll () ; queue. offer (x - 2) ; queue.offer (x) ; queue.offer (y + 4) ; System.out.println ("Queue Elements: ") ; while (! queue.empty() ) System.out.print (queue.poll () + "...
Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } } t is non-static and it cannot be referenced in a static context in the main method. The program compiles and runs fine. The variable t is not initialized and therefore causes errors. The variable x is not initialized and therefore causes errors.