• Post Reply Bookmark Topic Watch Topic
  • New Topic

multiple Methods  RSS feed

 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to figure out how to get information back from a different method.

Basically I want to write a program to create a car, but I want to be able to add features based on user our put (V6 versus V8, Color choice, etc), but I want each one to be broken out into their own method but I am unsure of how to call from the main method and return the variable (engineSize = V8, color = blue, etc). Can someone point me in the right direction (and pardon my noobness if I got the terminology wrong).

Thanks.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be helpful if you could post the code that you've come up with so far, so we can see if you're on the right track. It would also help a great deal in providing more conrete suggestions that will help you along.
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have actually not started coding it yet.... well other than notes I am taking a project and turning it into a learning opportunity. The project I have right now just says choose a car (1,2,3) and then outputs the actual name of the car. Now I want to take it a step further and add in options which would be in there own method.

Clear as mud?
 
Jhon Gonzales
Greenhorn
Posts: 22
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at Decorator Design Pattern
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so I threw together some quick code so you can visually see what I am trying to do (this is not complete by any means). I am just trying to get to the point where I can return the users feedback to the main body so it will be able to print out the choices they make.

Also this may not be the best way to do something but I am trying to teach myself some different parts of java programming by taking a project that I know what I want it to do and then create a program around the part of java I want to learn (in this case using multiple methods and returning the user input).

Thansk.

 
Tyson Lindner
Ranch Hand
Posts: 211
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just have to use variables in your main method to store the results of your other methods, like:

String manufacturer = Manufacturer();

Then to print results:
System.out.println(manufacturer);

As you get better acquainted with object oriented program you'll want to completely redesign your project using a Car class.
 
Kat Rollo
Ranch Hand
Posts: 62
Eclipse IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using the Scanner object incorrectly. Here is a quick tutorial.
You might want to read up on methods and parameters. Here is a tutorial from Oracle.

It doesn't mean main(String[] args) has a String[], you put String[] in every method parameter.
For user-defined methods, the things inside parenthesis (known as parameters) are optional and you only define them if the method needs it.
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, both answers put me on the right path.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!