Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

multiple Methods

 
Ranch Hand
Posts: 195
2
  • 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.
 
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 Hand
Posts: 195
2
  • 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?
 
Greenhorn
Posts: 22
PHP Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at Decorator Design Pattern
 
John Morgan
Ranch Hand
Posts: 195
2
  • 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.

 
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.
 
Ranch Hand
Posts: 62
Eclipse IDE MySQL Database Java
  • 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 Hand
Posts: 195
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, both answers put me on the right path.
 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic