Sign in to follow this  
Followers 0

Some help for Java (D.R.Y.)

2 posts in this topic

Hey, at the moment im doing a Java "Crash course" wich worked for now probably good. Since two days I am working on a exercie and dont know what to do to solve this problem. I would be glad if anyone of you could help / teach me what to do. If you want to try by yourself:

Please login or register to see this link.

(Im @ Java on 19%). Well I would be glad if you could explain me what D.R.Y. is.

___

Introducion:

Don't Repeat Yourself (D.R.Y)
The D.R.Y. principle is really important in programming. No repeating!

Any time you find yourself typing the same thing, but modifying only one small part, you can probably use a function.

The 'small part' that you find yourself modifying will be the parameter. And the part that you keep repeating will be the code in the reusable block - the code inside { }.

___

Instruction:

You are a creature of habit. Every week you buy 5 oranges. But orange prices keep changing!
 

  1. You want to declare a function that calculates the cost of buying 5 oranges.
  2. You then want to calculate the cost of the 5 all together.
  3. Write a function that does this called orangeCost().
  4. It should take a parameter that is the cost of an orange, and multiply it by 5.
  5. It should log the result of the multiplication to the console.
  6. Call the function where oranges each cost 5 dollars.

___

Hint:


What is the one bit of input that changes each time? It would be the price. So give your parameter the name price. And when you call your function, put in a number for the price to see how much 5 oranges cost!

Share this post


Link to post
Share on other sites

I haven't looked at the website yet, but the basics of java involve using methods (or functions as they are called in this guide, but for java they are generally called methods.). What this is trying to teach you is that you can create a method and use it multiple times in a program, so you don't have to type out a long equation each time you need to use it.

 

Example:

public int getOrangeCost( int cost ){    return cost * 5;}

5 - represents the static number of oranges

 

You use the method like this.

getOrangeCost( 1.00 )

If you would want to print the cost to the screen type something like this:

System.out.print( getOrangeCost( 1.00 ) );

Notice you can put what ever cost you want for the oranges.

 

EDIT: I didn't really mention anything about the acronym D.R.Y.. If your going to use the equation to solve for oranges more than once or twice make a method for it, so you don't have to type everything out for it again. It may not seem like a big deal it with such a small problem, but once you get to more advanced math operations and algorithms this becomes a very important part of programming.

1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0