Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 3rd, 2012, 12:01 PM
acidblue acidblue is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 80 acidblue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 54 m 17 sec
Reputation Power: 3
Please help me understand classes?

Why can't I do this:

Main:
Code:
public class TestInvestment {

	
	public static void main(String[] args) {
		
		FutureInvestmentValue fiv = new FutureInvestmentValue();
		fiv.investment(a,b,y);
	}

}


Class:
Code:
import java.util.Scanner;


public class FutureInvestmentValue {
	
	
	
	public double investment(double a, double b, int y){
	Scanner input = new Scanner(System.in);
	System.out.println("Enter invest amount ");
	a = input.nextDouble();
	System.out.println("Enter intrest rate ");
	b = input.nextDouble();
	System.out.println("Enter how many years ");
	y = input.nextInt();
	
	return a + (y*(a * b));
	
}

}


I'm getting an error in eclipse:
a cannot be resolved
b cannot be resolved
y cannot be resolved

Can't I pass the user input to the method??

Reply With Quote
  #2  
Old October 3rd, 2012, 12:24 PM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,886 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 22 m 27 sec
Reputation Power: 581
Java is pass-by-value. That means changing the values of parameters in a method will not be reflected back to the caller. While technically legal, the IDE is assuming you really are trying to use different variable names which you have not declared. You don't need to pass anything:
Code:
public class TestInvestment {

	
	public static void main(String[] args) {
		
		FutureInvestmentValue fiv = new FutureInvestmentValue();
		double result=fiv.investment();
	}

}

Code:
public class FutureInvestmentValue {
	
	
	
	public double investment(){
        double a;
        double b;
        int y;
	Scanner input = new Scanner(System.in);
	System.out.println("Enter invest amount ");
	a = input.nextDouble();
	System.out.println("Enter intrest rate ");
	b = input.nextDouble();
	System.out.println("Enter how many years ");
	y = input.nextInt();
	
	return a + (y*(a * b));
	
}
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.

Last edited by gw1500se : October 3rd, 2012 at 01:10 PM.

Reply With Quote
  #3  
Old October 3rd, 2012, 12:55 PM
acidblue acidblue is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 80 acidblue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 54 m 17 sec
Reputation Power: 3
OHHH, I was soo close, yet so far away.
I had too many parameters, DOH!

Thanks for the reply, this OOP is so new to me.

Reply With Quote
  #4  
Old October 3rd, 2012, 01:09 PM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,886 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 22 m 27 sec
Reputation Power: 581
Its a little hard to get your head around (at least it was for me) but once you do, you'll understand how it makes life easier.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Please help me understand classes?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap