Question

Q19 Consider the following main function: 10 Points public class Main { public static void main(String[] args) { Office offic

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

{ 1. public class Main 2 public static void main(String [largs) { 3 Office office = new Office(5421 sennott square); 4 offi

Screenshot of the code with output:

✓ Execute Mode, Version, Inputs & Arguments Execute Result CPU Time: 0.22 sec(s), Memory: 35132 kilobyte(s)compiled and execu

Add a comment
Know the answer?
Add Answer to:
Q19 Consider the following main function: 10 Points public class Main { public static void main(String[]...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){...

    Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next();    String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word);    if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...

  • What output is produced by the following program? public class MysteryNums public static void main(String[] args)...

    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)    {   ...

    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...

  • Correct the mistakes public class customer { public static void main(String[] args) { D customer=new d();...

    Correct the mistakes public class customer { public static void main(String[] args) { D customer=new d(); d.employee(56,”ali”); d.department(7,8.6,9); } public void employee(String name, int age) { System.out.println("Your name is"+name); System.out.println(“Age is”+age); } public int department(double d, double t, int a) { System.out.println("I have java exam at 3:00"); Return(1.1); } } V. PROGRAMMING. Write a complete JAVA program to implement the following. 1. Create a class named Exponent. Its main() method accepts an integer value from a user at the keyboard,...

  • 2016 points output of following Java Program? public class Base { public final void show() {...

    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) {...

    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...

    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;...

    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) {      ...

    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...

  • Revision Question Consider the following Java class: { public static void main (String [ ] args)...

    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 () + "...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT