How to pass a result between methods in Java - java

Im in CS 1301 and I am struggling with some homework. I am trying to pass the parameter result to the final method so I can just write result instead of 42, and its not recognizing it. Any help would be appreciated.
public class ICalledYou
{
public static void main(String[] args)
{
ProductNumber();
ProductBinary();
}
public static void ProductNumber()
{
Double x = 7.00;
Double y = 6.00;
Double result = y*x;
System.out.println(result);
}
public static void ProductBinary()
{
System.out.println(Integer.toBinaryString(result));
}
}

The method public static void ProductBinary() does not take a parameter. Change that,
public static void ProductBinary(double result)
Second
ProductBinary();
Isn't passing a parameter, change that
ProductBinary(42.0);

Change ProductNumber to return the result, then pass that to the other method:
public static void main(String[] args)
{
double result = ProductNumber();
ProductBinary(result);
}
public static double ProductNumber()
{
Double x = 7.00;
Double y = 6.00;
Double result = y*x;
return result;
}
public static void ProductBinary(double number)
{
System.out.println(Integer.toBinaryString(number));
}

Related

I wrote a method to count positive numbers, but how to call that in main method

public class positiveCount {
private static int countPositive(int[] elems) {
int positive = 0;
for (int i=0;i<elems.length;i++) {
if (elems[i] > 0){
positive++;
}
}
return positive;
}
//This gives me the number of the positive numbers.
public static void main(String[] args) {
}
}
in the main method, i want to enter a list, for example {3,4,5,-2,-3,0},how to call the method positveCount
If the main method is in the same class :
public static void main(String[] args) {
countPositive(new int[] {3,4,5,-2,-3,0});
}
Else (works also for case 1) :
public static void main(String[] args) {
PositiveCount.countPositive(new int[] {3,4,5,-2,-3,0});
}

How to call two methods in same class?

How can I call two methods from the same class over one object? I mean I try to write a class and its methods to run above code:
volume = Calculate.do_calc().get_volume(a);
I am creating Calculate class and two methods of it. do_calc() and get_volume(a). How should I write this class to run that code.
Unless do_calc() returns the class in where de function get_volume() is located this should never be done.
Here is a little sample for you.
public class ChainTest {
public static void main(String[] args) {
System.out.println(new ChainTest().do_calc().get_volume(1));
}
public ChainTest do_calc() {
// do something;
return ChainTest.this;
}
public int get_volume(int a) {
return a;
}
}
You don't need to write the code in one line. You can call same object methods in different lines.
Calculator calculator = new Calculator();
calculator.do_calc();
calculator.get_volume(a);
In case, if you want static methods
Calculator.do_calc();
Calculator.get_volume(a);
Case 1 If do_calc is static
public class Calculator {
public static void main(String[] args) {
System.out.println(Calculator.do_calc().get_volume(1));
}
public static Calculator do_calc() {
Calculator calculator = new Calculator();
// do something;
return calculator;
}
public float get_volume(int a) {
return a;
}
}
Case 2 : If do_calc is not static
public class Calculator {
public static void main(String[] args) {
System.out.println(new Calculator().do_calc().get_volume(1));
}
public Calculator do_calc() {
Calculator calculator = new Calculator();
// do something;
return calculator;
}
public float get_volume(int a) {
return a;
}
}
Case 3 : If both have return type float as you mentioned in comment
public class Calculator {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.do_calc();
System.out.println(calculator.get_volume(1));
}
public float do_calc() {
// do something;
return 1f; // return your result
}
public float get_volume(int a) {
// do something;
return a;
}
}
You must return this; at the end of each method of your class if they are not static. If the methods are static, do it like this:
public class Calculation {
public static Calculation do_calc () {
//do your calculation
return null;
}
public static Calculation get_volume(int x) {
//do your calculation
return null;
}
}
Then you can write:
Calculation.do_calc().get_volume(1);
No problem in returning null, as the methods are static and not related to a specific instance of the class. If you don't like it, then return new Calculation();
[Edit]
The first method should return a real object if you need to pass its result to the second method:
public class Calculation {
int result;
public static Calculation do_calc () {
//do your calculation
Calculation c=new Calculation();
c.result = theResultOfTheCalculation;
return c;
}
public void get_volume(int x) {
//do your calculation for example:
System.out.println(result + x);
}
}

Its not applicable for the arguments (int, String), but i have the correct format for it

I am just making a random class so that i can mess around and learn java with it. Ive kinda got this concept of arguments down.
public class OffensiveLine {
public static void main(String args[]){
String[] blocks = {"Swim Move", "Hello"};
LineMan jeff = new LineMan(80, 90);
int i = 1;
WideReciever.block(32, blocks[i]);
jeff.block();
}
}
public class WideReciever extends Speed{
static Date now = new Date(1);
public WideReciever() {
// TODO Auto-generated constructor stub
super();
}
public static void run(){
}
public static void block(int b, String[] wow){
int i = 1;
System.out.println(wow[i]);
}
}
public static void block(int b, String[] wow){
This method wants a String array.
However, you call it like this:
WideReciever.block(32, blocks[i]);
blocks is a String array, but blocks[i] is just a string.
You could either change the block method to take a string, or pass the array in:
public static void block(int b, String[] wow) {
System.out.println(wow[1]);
}
or:
WideReciever.block(32, blocks);
This
WideReciever.block(32, blocks[i]);
Is passing in an int and a String to a method which accepts an int and a String array. What you are likely trying to do is:
WideReciever.block(32, blocks);

How can access a object in main class from another class in java?

I have a main class and two sub class in java language.how can access xx in class Y?please help me ,I need it in my project.
import class X,Y;
public static void main(String[] args) {
xx=new X;
example=new Y;
}
public class Y{
change xx.value;//how can change it?
}
If I understand your question, then you can do it with an accessor and mutator (or getter and setter if you prefer) and something like,
static class X {
public X(int value) {
this.value = value;
}
int value;
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public String toString() {
return Integer.toString(value);
}
}
static class Y {
public void example(X x) {
System.out.println("in example");
x.setValue(x.getValue() + 5);
}
}
public static void main(String[] args) {
X xx = new X(10);
System.out.printf("xx = %s%n", xx);
Y yy = new Y();
yy.example(xx);
System.out.printf("xx = %s%n", xx);
}
Output is
xx = 10
in example
xx = 15
Create a function inside Y class with a type X parameter, then you can call this function from your Main class and change xx value.
import class X,Y;
public static void main(String[] args)
xx=new X;
example=new Y;
xx=example.changeXValue(xx);
}
public class Y{
public X changeXValue(X xx){
//change xx here
return xx;
}
}
make xx become parameter of method of class Y, because JAVA use reference by default (for object), so that if you change xx in method of class Y, it also apply to method main
import class X,Y;
public static void main(String[] args) {
xx=new X;
example=new Y;
example.modify(xx)
}
public class Y{
public void modify(X xx){ //change xx here)
}

Can't send string to another class from if loop

I'm trying to pass a string from one class to another, but not succeeding. I realized during research and trial and error that I need to have "public static void main(String[] args) {}" to be able to use the if statement, but then getY() produces an error. What can I do differently?
public class Testing {
public static String z;
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
public static String getZ() {
return z;
}
}
}
The other class is
class B {
public static void main(String args[]) {
String x = Klasatest2.getZ();
System.out.println(x);
}
}
Error:
Klasatest2.java:14: illegal start of expression
public static String getZ()
^
Klasatest2.java:14: illegal start of expression
public static String getZ() {
^
Klasatest2.java:14: ';' expected
public static String getZ() {
^
Klasatest2.java:14: ';' expected
public static String getZ() {
^
4 errors
For starters, you can't declare a method inside of a method,
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
public static String getZ() {
return z;
}
}
}
So you have to make sure that get getZ() method is declared OUTSIDE of main(string[] args)
Like this,
public class Test {
public static String z;
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
}
public static String getZ() {
return z;
}
}
Also, you shouldn't have two main(String[] args) methods, as only one of them will be called unless for some reason you decide to call it yourself, which would be very strange.
So if you wanted the string to be set in class Test, you would need to call it's main method from your other class, possible like this.
Test.main(null);
Your application can only have one main(String args[]) method.
Try this:
public class Testing {
public static void main(String[] args) {
A a = new A("hy");
B b = new B(a.z);
}
public class A {
public String z;
public A (String z) {
this.z = z;
}
}
public class B {
public B (String y) {
System.out.println(y);
}
}
}

Categories

Resources