Question

What value is printed out from the code below? public void square(double x) { double x...

What value is printed out from the code below?

public void square(double x) {
double x = x * x;
}
:
double val = 10;
square(val);
System.out.println(val);

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

Champ the above code needs some modification so that it functions appropriately.

Firstly, let us consider the statement double x = x * x;

You cannot store the resultant value(x*x) in x because it's already declared over here public void square(double x).

Secondly, the codehas to be implemented within the class.

The method public void square(double x) does not retrun any value. Hence either you have to print the output within the function or you can return the output to the function call.

From a static main function you cannot call a non-static method. Therefore the function public void square has to be implemented as a static one.

The code and it's output are shown below...

class Demo

{

public static void square(double x)

{

double y = x * x;

System.out.println(y);

}

  

public static void main(String args[])

{

double val = 10;

square(val);

}

  

}

Add a comment
Know the answer?
Add Answer to:
What value is printed out from the code below? public void square(double x) { double x...
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 code. It is valid. What is printed? public static void TestLabel() { int...

    Consider the following code. It is valid. What is printed? public static void TestLabel() { int i = 0; MyLabel: for (i++; i<20; i++) { if (i 10) continue MyLabel; System.out.println(i); } nothing (doesn't work) 20 10

  • What is the output of the following code? class C1 { public double PI=3.1; public void...

    What is the output of the following code? class C1 { public double PI=3.1; public void m1() { System.out.println("M1 of C1"); } } class C2 extends C1 { public double PI=3.14; public void m1() { System.out.println("M1 of C2"); } } public class C { public static void main(String[] args) { C1 obj = new C2(); System.out.print(obj.PI); } } 3.1 3.14 None of the above

  • What is the output of the following code? class C1 { public double PI=3.1; public void...

    What is the output of the following code? class C1 { public double PI=3.1; public void m1() { System.out.println("M1 of C1"); } } class C2 extends C1 { public double PI=3.14; public void m1() { System.out.println("M1 of C2"); } } public class C { public static void main(String[] args) { C1 obj = new C2(); obj.m1(); } } M1 of C1 M1 of C2 None of the above

  • Explain in detail what the code below does: public class MyClass {       public static void...

    Explain in detail what the code below does: public class MyClass {       public static void main(String args[]) {              System.out.println(isUniqueChars("something"));       }       public static boolean isUniqueChars(String str) {             int checker = 0;                                                                                               for (int i = 0; i < str.length(); ++i) {                         int val = str.charAt(i) - 'a';                         if ((checker & (1 << val)) > 0) return false;                         checker |= (1 << val);             }             return true;...

  • What are the first five lines printed from this code and why? public class LoopQ {...

    What are the first five lines printed from this code and why? public class LoopQ { public static void main(String[] args) { for(char c = 'A'; c < 'Z'; c++) { for(int i = 0; i < 2; i++) { System.out.println("seat "+c+i); } } } }

  • For the below code, what will be printed? public class mathchar { public static void main(String[]...

    For the below code, what will be printed? public class mathchar { public static void main(String[] args) { int i = 81, j = 3, k = 6; char w = 'f'; System.out.printf("%.2f\n", Math.sqrt(i)); System.out.printf("%.2f\n", Math.pow(j,k)); System.out.printf("%c\n", Character.toUpperCase(w)); System.out.printf("%c\n", Character.toLowerCase(w)); System.out.printf("%d\n", (int)(Math.random() * 21 + 6)); /* just tell range of possible values */ } }

  • Java. What is the output of this code? Ace.java public class Ace { private double a;...

    Java. What is the output of this code? Ace.java public class Ace { private double a; public Ace ( double x) {       a = x; } public void multiply (double f) {       a *= x;         } public String toString () {       return String.format ("%.3f", x); AceDem.java public class AceDemo { public static void main(String args[]) {    Ace card = new Ace (2.133); card.multiply (.5); System.out.println (card); } }

  • 47 What is printed out by the code listed below the classes (i.e., below the line)?...

    47 What is printed out by the code listed below the classes (i.e., below the line)? class Animal public virtual void whoAreYou () cout << "an Animal\n";h class Insect public Animal public void whoAreYou() cout < "an Insect\n"; class Spider public Insect public void whoAreYou() cout << "a Spider\n"; Animal* a1-new Animal; Animal* a2 = new insect ; Animal* a3 = new spider; al->whoAreYou ); a2->whoAreYou ); ??->whoAreYou ( ) ; an Insect, a Spider, a Spider O an Animal,...

  • **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args)...

    **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args) for (int k-1: k10:k if (k8) continue: System.out.println k) Question 2 3.5 pts Analyze the following code. Which one is correct? package p public class A package p2: import p1. public class B extends Af int i- 5 private int j- 10: public A0I public static void main(Stringll args) B b new B0: System.out.println(a.i+", "+ajt", "+b.it""+bj): a.i is printable None of them b.i is...

  • What is the output of the following code: public static void main(String []args){     int x =...

    What is the output of the following code: public static void main(String []args){     int x = 10;     int y = 10;     try{     System.out.println(function1(y, x));     } catch (Exception e) { System.out.println(“ERROR”); } } static int function1 (int x, int y) {     x += 10;     y += 7;     return (x + y); }

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