Accesor method is not recieving var from mutator method - java

I am making a basic calculator on eclipse, java. But I have a problem with one of the methods as it doesn't accept the right variable.
I know that the problem is in the calculateDifference() and setCurrentValue() method.
public class Dollar {
static int startingValue = 2650;
static int currentValue;
static int dollars;
static int differenceValue = calculateDifference();
static void setDollarQuantity (int dollarValue) {
dollars = dollarValue;
}
static void setCurrentValue(int currentDollar) {
currentValue = currentDollar;
}
static int calculateDifference() {
return ( currentValue - startingValue) * dollars;
}
public static void main(String[] args) {
setCurrentValue(2780);
setDollarQuantity(111);
calculateDifference();
}
}
The expected result from the calculateDifference method was 14,430 but the actual is 0. I have found the problem which was the calculateDifference method is not accepting the currentValue as 2780, but 0. Anyone can help me and modify my code?

Change
static int diffrenceValue = calculateDifference();
to
static int differenceValue;
and in main()
calculateDifference();
to
differenceValue = calculateDifference();
System.out.println(differenceValue);
This way you will set the differenceValue after the other variables are initialized with correct value, not before.

Related

Why is it an invalid method declaration when I am passing parameters?

public class Store {
// instance fields
int area;
// constructor method
public Calc(int one, int two, int three) {
area = one*two*three;
}
// main method
public static void main(String[] args) {
int sideOne = 2;
int sideTwo = 3;
int sideThree = 1;
Calc mult = new Calc(sideOne,sideTwo,sideThree);
System.out.println(mult.area);
}
}
Can anyone help a beginner understand why, when passing parameters, this is an invalid method declaration?
You define/call a Calc constructor, but there is no Calc class.
Rename your class to Calc ant your code will compile and execute correctly:
public class Calc {
// instance fields
int area;
// constructor method
public Calc(int one, int two, int three) {
area = one * two * three;
}
// main method
public static void main(String[] args) {
int sideOne = 2;
int sideTwo = 3;
int sideThree = 1;
Calc mult = new Calc(sideOne, sideTwo, sideThree);
System.out.println(mult.area);
}
}

How can I pass a value from one constructor to another constructor?

I'm working with Java. I have a class with 2 constructors. The first constructor takes an int value as a parameter and sets an int variable as that value. The second constructor takes a string and prints it out. The idea is that when I call the first constructor from my main class, it sets an integer value. And when I call the second constructor in the main class, it takes the string representation of int variable of the first constructor and prints it out.
Here's how I made the constructors:
public class Test
{
int val;
public Test(int x)
{
val = x;
return val; //I know this won't work. So I'm looking for an alternative
}
public Test(String y)
{
System.out.println("The value is " + y);
}
}
And the main method (in a different class) looks like this:
public static void main(String [] args)
{
Test t1 = new Test(6);
Test t2 = new Test(String.valueOf(t1)); //This won't work because the first constructor can't return a value
}
So how exactly can I change the contents of the constructors so that I can pass val into the 2nd constructor?
Override toString() to return value so when you so String.valueOf(t1) it will do the toString() method;
public class Test
{
int val;
public Test(int x)
{
val = x;
}
public Test(String y)
{
System.out.println("The value is " + y);
}
#Override
public String toString()
{
return String.valueOf(val);
}
}
I think what you are probably actually trying to do is to override the toString() method of Test.
public class Test
{
int val;
public Test(int x)
{
val = x;
}
#Override
public String toString() {
return "Test:"+val;
}
}
Then you can do this:
public static void main(String [] args)
{
Test t1 = new Test(6);
String s = t1.toString();
// or this
System.out.println( t1 ); // prints "Test: 6"
}
What you're describing is actually impossible without some changes.
First and foremost, t1 and t2 are two separate instances and the values inside of them have no bearing on one another. So t1 has x=6 and t2 has x=0 (because of default values).
If you want your second constructor to have a value of x that isn't 0, then you'll need to pass that in too.
public Test(int x, String s) {
super(x);
System.out.println(x);
}
I think you don't really want two constructors. It seems like you're wanting to do something like the following:
public class Test
{
int val;
public Test(int x)
{
val = x;
}
public void printVal()
{
System.out.println("The value is " + val);
}
public static void main(String [] args)
{
Test t1 = new Test(6);
t1.printVal();
}
}
Your requirement is kinda weird. But this will work even it is kinda weird
public class Test {
private static int val;
public Test(int x) {
val = x;
}
public Test() {
System.out.println("The value is " + String.valueOf(val));
}
public static void main(String[] args) {
Test t1 = new Test(6);
Test t2 = new Test();
}
}

when a method should return a value java

I am a new user for java and I am a little confused on whether a method should return a value or not in java for example I wrote this simple method
public static void increase_user() {
int number=3;
if(number<10)
user++;
}
public static void main(String[] args) {
int user=10;
increase_user();
System.out.println(user);
}
Should I make increase_user method return a value in this case? and when it should return a value?
1.) You need to pass the user object.
2.) Also need to return the user object.
public static int increase_user(int user) {
int number = 3;
if (number < 10)
user++;
return user;
}
public static void main(String[] args) {
int user = 10;
user = increase_user(user);
System.out.println(user);
}
Making User variable static, works same as above, no need to pass/return user object
public class Test2 {
static int user = 10;
public static void increase_user() {
int number = 3;
if (number < 10)
user++;
}
public static void main(String[] args) {
increase_user();
System.out.println(user);
}
}
it's a case by case answer. Return a value when you need one. e.g. addUser() might return a new User object. setUserName() probably doesn't have a return value.
In you case, perhaps return a boolean to indicate whether the value was increased or not?

I can't get the exact method calling for this code

I'm trying to write a simple code in Java but I keep getting error for the method calling.
package tutorialproject2;
import java.util.Scanner;
public class Tutorialproject2 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
InputTest();
Calculate();
}
public static void InputTest(){
String message = input.nextLine();
System.out.println(Hello(message));
}
public static String Hello(String message){
if (message.equals("Hi")){
return "Hello";
}else{
return "Goodbye";
}
}
public int Calculate(int a,int b){
a = input.nextInt();
b = input.nextInt();
int answer = a * b;
return answer;
}
You have the method Calculate(int a,int b) with 2 parameters but call the method without parameters Calculate().
I suppose you should change the method Calculate(int a,int b) to
public static int Calculate(){
int a = input.nextInt();
int b = input.nextInt();
int answer = a * b;
return answer;
}
and as #Visme mentioned, to add static keyword.
or you can leave your method as
public int Calculate(){
int a = input.nextInt();
int b = input.nextInt();
int answer = a * b;
return answer;
}
In this case in the main function, you should call the method this way:
new Tutorialproject2().Calculate();
Non static method calculate is called from static function (main)
should be in this way, int var = Calculate(3,5); , can't be calculate() alone as it has a return type and arguments.
You could call methods with return type void alone like InputTest();
as you return integer from this method and the method has parameters so you should pass the required parameters here (of integer type),
public static int Calculate(int a,int b){
. . .
return answer;
}

Java 'this' keyword

I'm just beginning in programming and I'd like to make exercise from a book, but I can't. That's my problem:
public class increment {
int increment() {
return this + 1; // aka this++
}
public static void main(String[] args) {
int a = 0;
System.out.println(a.increment());
}
}
As you for sure guessed already, that it doesn't works, I want to ask you how to get outputed integer a incremented by one, but using keyword 'this'.
Regards and sorry for stupid questions.
It is strange to name a class like a method.
I guess you wanted this:
public class Counter {
int val;
public Counter (int start) {
val = start;
}
public void increment() {
val ++;
}
public String toString () {
return Integer.toString (val);
}
public static void main(String[] args) {
Counter counter = new Counter (0);
counter.increment ();
System.out.println(counter.toString ());
}
}
this is an object (the current object). You cannot "increment" it.
A way to do it is:
public class Increment {
int a = 0;
int increment() {
return a + 1;
// or: return this.a + 1;
// or: a++; return a; if you want a to be incremented from now on
}
public static void main(String[] args) {
Increment inc = new Increment();
System.out.println(inc.increment());
}
}
The this keyword in Java refers to the current scope's object instance. I don't think it's what you're looking for in this case.
In your example, a isn't an object of the class increment, it is a primitive int. In order to use the .increment() function you defined, it would have to be an object of type increment.
One option that may be what you're looking for would be the following.
public class Increment { //Java likes capitalized class names
private int myInt;
public Increment(int a) { //constructor
myInt = a;
}
public int increment() {
return ++myInt;
}
public static void main(String[] args) {
Increment a = new Increment(0);
System.out.println(a.increment());
}
}
In this example, we make a new class of type increment, which internally contains an integer. Its increment method increments that internal integer, and then returns the number.
you are using operator + for your current object (this). Operator overloading is not supported in java.
Something like this will work:
class MyInteger {
private int internal;
public MyInteger( int value ){
this.internal = value;
}
public int incerment(){
return ++this.internal;
}
}
public class Increment {
public static void main(String[] args) {
MyInteger a = new MyInteger(0);
System.out.println(a.increment());
}
}
You see, you can only implement methods for your own classes, not for existing classes, or for primitives like int.
i don't think you can use this to return the value, except if you're making a new class like this:
class Increment1
{
private int a;
public int increment2(int a)
{
this.a=a;
return this.a + 1;
}
}
public class Increment
{
static Increment1 b = new Increment1();
public static void main(String[] args)
{
int a = 0;
System.out.println(b.increment2(a));
}
}
You cannot increment a class like this.
You have to use a member variable that you can increment.
public class Test {
private int var;
public Test(int i) {
this.var = i;
}
int increment() {
this.var++;
}
}
public static void main(String[] args) {
Test t = new Test(0);
System.out.println(t.increment());
}
This refers to the current instance of the class, not a particular member.
You want to increment a property (I'm guessing of type long or int), and not the instance of your increment class (should be Increment, by the way).
Something like this would work:
public class increment {
private int innerValue = 0;
int increment() {
innerValue+=1
return innerValue; // aka this++
}
public static void main(String[] args) {
increment a = new increment()
System.out.println(a.increment());
}
}

Categories

Resources