Purchase Solution

Java Static Methods and Counting Numbers of Objects

Not what you're looking for?

Ask Custom Question

public class Syntax2
{
private int x, n;

public Syntax2(int a, int b)
{
x = a;
n = b;
}

public int sumofsquares(int x, int y)
{
return x * x + y * y;
}

public int nTimesProduct(int x, int y)
{
return n * x * y;
}

//test program
public static void main(String[] args)
{
Keyboard kbd = new Keyboard();
System.out.println("Enter two numbers ");
int m = kbd.readInt();
int n = kbd.readInt();
System.out.println("The result is: " + sumofsquares(m, n));
System.out.println("The other result is: " + nTimesProduct(m, n));
}

}

The above code has the following two errors:

Syntax2.java:39: non-static method sumofsquares(int,int) cannot be referenced from a static context: System.out.println("The result is: " + sumofsquares(m, n));
Syntax2.java:40: non-static method nTimesProduct(int,int) cannot be referenced from a static context: System.out.println("The other result is:" + nTimesProduct(m, n));

How do you rewrite this code so that it compiles and runs correctly, without making the sumofsquares and nTimesProduct methods static.

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

Assume you have a class called Employee whose constructor takes two arguments: the employee's name and id number. Consider the following code fragment:

Employee [ ] ea = new Employee[20];
Employee emp1 = new Employee("Bob Jones", 34532);
Employee emp2 = emp1;
ea[0] = emp1;
ea[1] = emp2;

How many Employee objects exist in the heap in the above code? _______

What is the total number of all objects in the heap in the above code? _______

Purchase this Solution

Solution Summary

This solution uses java class static methods and how to count the number of objects on the heap

Solution Preview

public class Syntax2
{
private int x, n;

public Syntax2(int a, int b)
{
x = a;
n = b;
}

public int sumofsquares(int x, int y)
{
return x * x + y * y;
}

public int nTimesProduct(int x, int y)
{
return n * x * y;
}

//test program
public static void main(String[] args)
{
Keyboard kbd = new Keyboard();
System.out.println("Enter two numbers ");
int m = kbd.readInt();
int n = kbd.readInt();
System.out.println("The result is: " + sumofsquares(m, n));
System.out.println("The other result is: ...

Purchase this Solution


Free BrainMass Quizzes
C# variables and classes

This quiz contains questions about C# classes and variables.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.