I am writing code in Java which has multiple methods and these methods have multiple variables. I want the other methods to access the variables of another method using actual and formal parameters. How can I do it?
I am pasting an example of the problem I'm facing.
Error : variable is not defined.
Code
public class example {
public void addition() {
int a = 0;
int b = 10;
int c = a + b;
}
public void result() {
System.out.println("The result for the above addition is" + c);
}
}
IM GETTING AN ERROR SAYING VARIABLE IS NOT DEFINED
You should declare c as global variable
public class Example {
int c;
public void addition() {
int a = 0;
int b = 10;
c = a + b;
}
public void result() {
System.out.println("The result for the above addition is " + c);
}
public static void main(String[] args) {
Example e = new Example();
e.addition();
e.result();
}
}
well, your java syntax is quite wrong... if you need to do an addition, you can do as follows:
public class Addition {
public static int addition(int a, int b)
{
int c= a + b;
return c;
}
public static void main(String[] args) {
int a = 1;
int b = 10;
int c = addition(a,b);
System.out.println("The result for the above addition is " + c);
}
}
where addition function does add a + b and return the result to your main method.
Related
Make a util class with three methods. First method's name is add, that returns the result by adding two numbers.
Second method's name is multiply, that returns the result by multiplying two numbers.
Third's method name is division that returns a result by dividing first parameter by second parameter.
Then call a method from this class called main and print the results 6 times for each method call? I'm having trouble with this part.
This is what I've got so far:
public class Util {
public static int add(int a, int b) {
int result = a + b;
System.out.println("result = " + result);
return result;
}
public static int multiply(int a, int b) {
int result = a * b;
System.out.println("result = " + result);
return result;
}
public static int divide(int a, int b) {
int result = a / b;
System.out.println("result = " + result);
return result;
}
public static void main(String[] args) {
int res = multiply(5, 2);
System.out.println("res = " + res);
res = add(5, 2);
System.out.println("res = " + res);
res = divide(5, 2);
System.out.println("res = " + res);
}
}
How would I do this: call a method from this class called main and print the results 6 times for each method call?
just use a for loop like that:
public static void main(String[] args) {
for (int i = 0; i <= 6; i++) {
System.out.println(multiply(5, 2));
System.out.println(add(5, 2));
System.out.println(divide(5, 2));
}
}
or from another class:
public class Main()
{
private Util myUtil;
public void calculate()
{
for (int i = 0; i <= 6; i++) {
System.out.println(myUtil.multiply(5, 2));
System.out.println(myUtil.add(5, 2));
System.out.println(myUtil.divide(5, 2));
}
}
public static void main(String[] args) {
Main myMain = new Main();
myMain.calculate();
}
I have a program to find pythagorean triples. in it, i have an object that needs to be used to call methods. Said object is broken. Errors are " The method Triples(int) is undefined for the type Triples" and "The method greatesCommonFactor() is undefined for the type Triples" mind you, not everything in Triples does useful stuff atm. It isn't completely finished yet.
public class TriplesRunner
{
public static void main(String args[])
{
int number;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the natural number :: ");
number=keyboard.nextInt();
Triples test = new Triples();
test.Triples(number);
test.greatestCommonFactor(number);
System.out.println(test.toString());
}
}
public class Triples
{
public int number;
public Triples(int num)
{
setNum(number);
}
public void setNum(int num)
{
int a = 0;
int b = 0;
int c = 0;
}
public int greatestCommonFactor(int a, int b, int c)
{
int max = 0;
for(a=1; a<=number-2; a++)
{
for(b=a+1; b<=number-1; b++)
{
for(c=b+1; c<=number; c++)
{
if(a*a + b*b == c*c);
}
}
}
return 1;
}
public String toString()
{
String output="";
output+="a + b + c";
return output+"\n";
}
}
you are trying to call the constructor as a method,
Change this part:
Triples test = new Triples();
test.Triples(number);
to
Triples.test = new Triples(number);
Triples isn't a method - it's your constructor, meaning it's invoked with the new operator:
Triples test = new Triples(number);
greatestCommonFactor is not defined properly. It currently takes three int arguments, instead of taking none and using Triples' data members:
public int greatestCommonFactor()
I was trying to make a program that searches for a random number, but i had problems importing the "a" variable in the other method. I would be happy if i could get some explanation. I have already tried to make the variable static, but that doesn't work
import java.util.Random;
public class verschlüsselung {
private static void nummber(int a) {
Random r = new Random();
a = r.nextInt(999);
System.out.println(a);
}
private static void search(int b) {
b = 0;
if(b =! a) {
for(b = 1; b =! a ; b++) {
if(b == a) {
System.out.println("found the number " + b);
}
}
}
}
public static void main(String args[]){
nummber(0);
search(0);
}
}
There is no such thing as using local variables in other methods.
You can return the variable from one method. Than call this method from other and get there the variable.
Declare the variable 'a' to be static and remove the parameter 'a' passed in the nummber()
function. This function does not need any input as it assigns the value of a random number to the global static variable 'a' which is accessed in the method search().
your declaration and method signature should read :
private static int a;
private static void nummber(){....}
May this help you:
private static int nummber( int a){
Random r = new Random();
a =r.nextInt(999);
System.out.println(a);
return a;
}
private static void search(int b, int a){
b = 0;
if(b =! a){
for(b =1; b =! a ; b++){
if(b == a){
System.out.println("found the number " + b);
}
}
}
}
public static void main(String args[]){
int a = nummber(0);
search(0, a);
}
I have a method returning an integer. My question is how to get this number
like
int d = returning value of customerperminute methods;
Thanks in advance
public int customerperminute(){
Random R = new Random();
int r = R.nextInt(4-0);
if (r==0||r==1){
return 0;
}
if (r==2){
return 1;
}
else {
return 2;
}
}
Just invoke method like this: int d = customerperminute();
suppose the class in which this method is defined is ABC and let's say customerperminuteStat, which is static method.
class ABC{
public int customerperminute(){
Random R = new Random();
int r = R.nextInt(4-0);
if (r==0||r==1){
return 0;
}
if (r==2){
return 1;
}
else {
return 2;
}
}
public static int customerperminuteStat(){
Random R = new Random();
int r = R.nextInt(4-0);
if (r==0||r==1){
return 0;
}
if (r==2){
return 1;
}
else {
return 2;
}
}
}
this is just class definition with it's methods.
To retrieve value from the function is to call the method.
*Static Method, say customerperminuteStat can be called from any type method(static/instance). But the non-static, say instance eg. customerperminute can be called from only instance methods.*
Calling
from the method of same class
int d = customerperminute();
int e = customerperminuteStat();
from the method of some other class
int d = customerperminute();
int e = customerperminuteStat();
1)Invoke it directly in non-static methods:
int value = customerperminute();
2)Invoke it in static method, add static in the method signature:
public class Test {
public static void main(String[] args) {
System.out.println(customerperminute());
}
public static int customerperminute() {
Random R = new Random();
int r = R.nextInt(4 - 0);
if (r == 0 || r == 1) {
return 0;
}
if (r == 2) {
return 1;
} else {
return 2;
}
}
}
use the following code in main() method
public static void main(String s[]){
ABC inst = new ABC(); //create instance of class which contains called method
int d = customerperminute();
}
or make customerperminute() as static & you can directly call it using class name ABC.customerperminute() if method is in different class or directly as customerperminute() if method is in same class as main() method
Can you help me find my error?
I'm trying to use these two methods here but my output is not working.
class Nine {
public static void Nine(String[] args) {
int x,y,z;
y = 3;
x = 7;
z = addEm(a, b);
System.out.println("answer= " +x);
}
public static addEm (double a, double b){
int c;
c = a+b;
}
}
Actually there are a lot of error in your code:
z=addEm(a, b);
here a and b are meaningless, you should use z=addEm(y,x); (if your intent is to sum three with seven)
System.out.println("answer= " +x);
I guess that you want to show the the results of the sum, therefore you should print z (and not x), so you should substitute with System.out.println("answer= " +z);
public static addEm (double a, double b) {
Here you missed the return type, and you need to consider also the type of parameters a and b. Since y,x and z are int, it is better if also a and b are int, and therefore specify also the return type as int:
public static int addEm (int a, int b) {
Or you can declare everything (y,x,z,a,b and return type) as a double: the important here is that they should be all of the same type. Moreover you miss also the return statement of the function addEm, that summarizing becomes:
public static int addEm (int a, int b)
{
int c;
c=a+b;
return c;
}
And finally also the function
public static void Nine(String[] args)
it is not right named for an entry point: its names should be main.
So in conclusion, if you apply all the fix (by modifying as less as possible your original code) a code that compile, run and works following some 'logic' is:
class Nine {
public static void main(String[] args) {
int x, y, z;
y = 3;
x = 7;
z = addEm(y, x);
System.out.println("answer= " + z);
}
public static int addEm(int a, int b) {
int c;
c = a + b;
return (c);
}
}
Man, this is a very basic java lesson:
every prog need an entry point, which is in java:
public static void main(String args[]){}
And then your code will execute.
You're passing arguments a and b to addEm, but those variables aren't initialized. I'm expecting you wanted to pass x and y instead.
class Nine
{
public static void Nine(String[] args)
{
int x,y,z;
y=3;
x=7;
z=addEm(x, y);
System.out.println("answer= " +x);
}
public static addEm (double a, double b)
{
int c;
c=a+b;
}
}
Your code will not work because your addEm method does not have any return type. In addition, the method you wrote takes Double params but while using you are trying to pass int to it. You also do not have any main method. I am assuming you misspelled or misunderstood the main method so below is the code which should work
class Nine
{
public static void Main(String[] args)
{
int x,y,z;
y=3;
x=7;
z=addEm(x, y);
System.out.println("answer= " + x);
}
public static int addEm (int a, int b)
{
int c;
c=a+b;
return c;
}
}