Question

With this class: public class myFunction { public int x = 7; public int y =...

With this class: public class myFunction { public int x = 7; public int y = 3; } 1. What are the class variables? 2. What are the instance variables? 3. What is the output from the following code: myFunction a = new myFunction(); myFunction b = new myFunction(); a.y = 5; b.y = 6; a.x = 1; b.x = 2; System.out.println("a.y = " + a.y); System.out.println("b.y = " + b.y); System.out.println("a.x = " + a.x); System.out.println("b.x = " + b.x); System.out.println("myFunction.x = " + myFunction.x);

Homework Answers

Answer #1
1. What are the class variables?
class variable is any variable declared with the static modifies.
There are no class variables in the given code.

2. What are the instance variables?
Variables x and y are instance variable.

3. What is the output from the following code:
System.out.println("myFunction.x = " + myFunction.x);
This lines gives compile time error.
Because x is not a class variable to access directly with class name.
Output of the code other than the above line is
a.y = 5
b.y = 6
a.x = 1
b.x = 2
Know the answer?
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
Similar Questions
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX;...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX; y= initialY; } public boolean equals (Object o){ if (o instanceof Point){ Point other = (Point)o; return (x == other.x && y == other.y); }else{ return false; } } } We haev defined "equals" method for our class using "instanceof". We define and use instances (or objects) of this class in the following scenarios. In each case, specify what is the output. (hint: there...
Java public class Point {      protected int x;      protected int y;      public Point...
Java public class Point {      protected int x;      protected int y;      public Point (int x, int y) {           this.x = x;           this.y = y;      }      public int getX() {           return x;      }      public int getY() {           return y;      } } THE FOLLOWING 3 QUESTIONS REFER TO THE CODE FRAGMENT ABOVE     Based on the code above, the Point Class is immutable: (a) TRUE (b) FALSE Explain: ___________________________________________ Subclasses...
class Ex1{ 2. public static void main(String args[]){ 3. int x = 10; 4. int y...
class Ex1{ 2. public static void main(String args[]){ 3. int x = 10; 4. int y = new Ex1().change(x); 5. System.out.print(x+y); 6. } 7. int change(int x){ 8. x=12; 9. return x; 10. } 11. } Can you please explain this entire code and what is happening?
what output is produced by the following code and explain how it works. public class A...
what output is produced by the following code and explain how it works. public class A { int a = 1; int b = 2; public int getSum(int a, int b) {     this.a+=a;     this.b+=b;     return this.a + this.b; } } public class B extends A { int a = 3; int b = 4; public int getSum(int a, int b) {     this.b=a;     super.b=b+b;     return super.a+this.b; } } public class q2 { public static void...
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A...
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B()...
What is the output of the following code segment? public class Exception { ... static int...
What is the output of the following code segment? public class Exception { ... static int divider(int x, int y) { try { return x/y; } catch (ArrayIndexOutOfBoundsException a) { System.out.print("A"); return 1; } } static int computer(int x, int y) { try { return divider(x,y); } catch (NullPointerException b) { System.out.print("B"); } return 2; } public static void main(String args[]){ try { int i = computer (100, 0); } catch (ArithmeticException c) { System.out.print("C"); } } } ABC A...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) {...
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) { return 0; } public static int[] convert1(String str) { return new int[1]; } public static String convert2(int[] v) { return ""; } public static void main(String[] args) { testSmallest(); testConvert(); } public static void testSmallest() { System.out.println("Testing your method smallest(...)"); int[][] testVectors1 = new int[][]{{1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3}, {2, 3, 4}}; int[][] testVectors2 = new...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Java question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class...
Java question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class Test { 5 public static void main ( String [] args ) { 6 SnoopDogg myCount = new SnoopDogg (); 7 myCount.count = 0; 8 int times = 0; 9 increment( myCount, times ); 10 System.out.println( myCount.count ); 11 System.out.println( times ); 12 } 13 public static void increment (SnoopDogg sd, int times ) { 14 sd.count = sd.count + 1; 15 times =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT