Skip to main content
In this program, you’ll see how to call functions. Functions are chunks of code with a name, and “calling” a function means to call on the function to do a task for you.Files NeededSample Output
1. Butterfly
2. Elephant
3. Teddy Bear
4. Snake

Which animal to draw? 1
Finish the program provided. You’ll need to write five if statements and some function calls. If you do it right it should display a phrase in an interesting way.Files NeededSample Output
I pledge allegiance to the flag.
In this program, you’ll look at a function that “returns a value”. When you call on the function to do a task, it will give you back a result.Files NeededSample Output
A triangle with sides 3,3,3 has an area of 2.0
A triangle with sides 3,4,5 has an area of 6.0
A triangle with sides 7,8,9 has an area of 26.832815729997478
A triangle with sides 5,12,13 has an area of 30.0
A triangle with sides 10,9,11 has an area of 42.42640687119285
A triangle with sides 8,15,17 has an area of 60.0
Write a function to compute the distance between two points. Given two points (x1, y1) and (x2, y2), the distance between these points is given by the formula: Distance FormulaYou must name the function distance, and it must return a double giving the distance between the two points.Files NeededSample Output
(-2,1) to (1,5) => 5.0
(-2,-3) to (-4,4) => 7.280109889280518
(2,-3) to (-1,-2) => 3.1622776601683795
(4,5) to (4,5) => 0.0
Write a function. It will return the name of a month of the year, given the month number, according to the table below. Make sure you do not put any input or output statements in the function; the month number will be passed in and the string containing the name will be returned.
NumberMonth
1January
2February
3March
4April
5May
6June
7July
8August
9September
10October
11November
12December
anything elseerror
The function must be called month_name(), and must have one parameter (an integer), and return a String.And finally, here’s some starter code.Files NeededSample Output
Month 1: January
Month 2: February
Month 3: March
Month 4: April
Month 5: May
Month 6: June
Month 7: July
Month 8: August
Month 9: September
Month 10: October
Month 11: November
Month 12: December
Month 43: error
Write a function to give you the “month offset” given a number representing the month. You can get the month offset from the following table:
MonthMonth Offset
11
24
34
40
52
65
70
83
96
101
114
126
anything else-1
Of course, here’s the main() to test your function.Files NeededSample Output
Offset for month 1: 1
Offset for month 2: 4
Offset for month 3: 4
Offset for month 4: 0
Offset for month 5: 2
Offset for month 6: 5
Offset for month 7: 0
Offset for month 8: 3
Offset for month 9: 6
Offset for month 10: 1
Offset for month 11: 4
Offset for month 12: 6
Offset for month 43: -1
Using the functions you wrote in previous assignments and the leap year function provided, write a function to determine the day of the week a person was born given his or her birthday. The following steps should be used to find the day of the week corresponding to any date from 1901 through the present.In the following explanation, the following terms will be helpful. Assuming I type in my birthday as “6 10 1981”:The month is 6. The day of the month is 10. The year is 1981.
  1. Find the number of years since 1900, and put it into a variable called yy. Simply subtract 1900 from the year to get this.
  2. Divide the number of years since 1900 by 4. Put the quotient in a variable called total. For example, if the person was born in 1983, divide 83 by 4 and store 20 in total.
  3. Also add the number of years since 1900 to total.
  4. Add the day of the month to total.
  5. Using the function month_offset() you wrote, find the “month offset” and add it to total.
  6. If the year is a leap year and if the month you are working with is either January or February, then subtract 1 from the total. You can use the function is_leap() provided to determine if the year is a leap year.
  7. Find the remainder when total is divided by 7. Pass this remainder to the function weekday_name() you wrote to determine the day of the week the person was born.
  8. Finally, build up a single String value containing the whole date (day of week, month, day, year). You’ll need to use the function month_name() you wrote to show the month name rather than its number.
  9. Return that String value.
Whew. Here’s some code to get you started.Files NeededSample Output
Welcome to Mr. Mitchell's fantastic birth-o-meter!

All you have to do is enter your birthday, and it will tell you
the day of the week on which you were born.

Some automatic tests....
12 10 2003 => Wednesday, December 10, 2003
 2 13 1976 => Friday, February 13, 1976
 2 13 1977 => Sunday, February 13, 1977
 7  2 1974 => Tuesday, July 2, 1974
 1 15 2003 => Wednesday, January 15, 2003
10 13 2000 => Friday, October 13, 2000

Now it's your turn!  What's your birthday?
Birthday (mm dd yyyy): 11 11 2010

You were born on Thursday, November 11, 2010!
Write a program to calculate the area of four different geometric shapes: triangles, squares, rectangles, and circles. You must use functions. Here are the functions you should create:
public static double area_circle( int radius )              // returns the area of a circle
public static int area_rectangle( int length, int width )   // returns the area of a rectangle
public static int area_square( int side )                   // returns the area of a square
public static double area_triangle( int base, int height )  // returns the area of a triangle
Your program should present a menu for the human to choose which shape to calculate, then ask them for the appropriate values (length, width, radius, etc.). Then it should pass those values to the appropriate function and display the resulting area.Notice that you must not input the values inside the functions, and you must not display the values inside the functions. All input and output must be in the main(), and values must be passed to the functions and returned from them.
ShapeFormula
squareA=s2A = s^2
rectangleA=l×wA = l \times w
triangleA=12×bhA = \frac{1}{2} \times bh
circleA=πr2A = \pi r^2
You’ll need the value of π\pi for area_circle(); feel free to use the built-in double variable called Math.PI.The menu should keep looping until the human chooses to quit.Sample Output
Shape Area Calculator version 0.1 (c) 2005 Mitchell Sample Output, Inc.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

1) Triangle
2) Rectangle
3) Square
4) Circle
5) Quit
Which shape: 1

Base: 5
Height: 6

The area is 15.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

1) Triangle
2) Rectangle
3) Square
4) Circle
5) Quit
Which shape: 2

Length: 10
Width: 4

The area is 40.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

1) Triangle
2) Rectangle
3) Square
4) Circle
5) Quit
Which shape: 3

Side length: 9

The area is 81.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

1) Triangle
2) Rectangle
3) Square
4) Circle
5) Quit
Which shape: 4

Radius: 4

The area is 50.2655.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

1) Triangle
2) Rectangle
3) Square
4) Circle
5) Quit
Which shape: 5

Goodbye.
Download and finish the following code to practice writing function calls.Files NeededSample Output
Ant Banana Crocodile Doggie Elephant Frog Gorilla Horseradish
Ice_cream Jackrabbit Kiwi Lhasa_apso Monkey! Narwhal Orangutan Parrot Quail
Rabbit Snake Thyme Ugli_fruit Valentine_candy Walrus X_men Yams Zebra
Note that this exercise only requires you to write function calls. The function implementations are already complete. If you do everything correctly, it should produce output like the following:
Download and finish the following code to practice working with functions:Files NeededIf you do everything correctly, it should produce output like the following:Sample Output
Watch as we demonstrate functions.

I'm going to get a random character from A-Z
The character is: N (or whatever)

Now let's count from -10 to 10
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 How was that?

Now we take the absolute value of a number.
|-10| = 10

That's all.  This program has been brought to you by:

programmed by Graham Mitchell
modified by [your name here]
This code is distributed under the terms of the standard BSD license.  Do with i
t as you wish.
Download and finish the following code to practice working with functions:Files NeededIf you do everything correctly, it should produce output like the following:Sample Output
Here we go.

Some random numbers, if you please:
First: 2
Second: 8
They were not the same.

More counting, but this time not by ones: 2 4 6 8 10 8 6 4 2
Let's figure a project grade.
434521 -> 71

Finally, some easy ones.Please enter your name: jose
Hi, jose

Do you feel lucky, punk?
you lose
Write a program that pulls up a menu with 4 options. It should look something like…Sample Output
Ye Olde Keychain Shoppe

1. Add Keychains to Order
2. Remove Keychains from Order
3. View Current Order
4. Checkout

Please enter your choice: 1

ADD KEYCHAINS

1. Add Keychains to Order
2. Remove Keychains from Order
3. View Current Order
4. Checkout

Please enter your choice: 3

VIEW ORDER

1. Add Keychains to Order
2. Remove Keychains from Order
3. View Current Order
4. Checkout

Please enter your choice: 4

CHECKOUT
  • You will need to create functions for each of the 4 menu options. Entering the number will call the correct function.
  • This assignment does not require you to complete ANY of the functionality except for the working menu system. There is no need for you to program the ability to add keychains, remove keychains, view orders or checkout.
  • The functions should be named add_keychains(), remove_keychains(), view_order() and checkout().
  • Each function should print a message that it has been called.
  • The user should be able to keep putting in choices until the checkout() function is called. When checkout() is finished, the program should end.
Okay, now it is time to make the keychain shop actually work.Sample Output
Ye Olde Keychain Shoppe

1. Add Keychains to Order
2. Remove Keychains from Order
3. View Current Order
4. Checkout

Please enter your choice: 1

You have 0 keychains. How many to add? 3
You now have 3 keychains.

1. Add Keychains to Order
2. Remove Keychains from Order
3. View Current Order
4. Checkout

Please enter your choice: 2

You have 3 keychains. How many to remove? 1
You now have 2 keychains.

1. Add Keychains to Order
2. Remove Keychains from Order
3. View Current Order
4. Checkout

Please enter your choice: 3

You have 2 keychains.
Keychains cost $10 each.
Total cost is $20.

1. Add Keychains to Order
2. Remove Keychains from Order
3. View Current Order
4. Checkout

Please enter your choice: 4

CHECKOUT

What is your name? Biff
You have 2 keychains.
Keychains cost $10 each.
Total cost is $20.
Thanks for your order, Biff!
  • You will need 2 new variables in main, one to store the current number of keychains, and one to store the price per keychain.
  • The price should be $10 per keychain.
  • add_keychains() will need to be passed one int, and have a return type of int. It will ask the user for the number of keychains to add to the order, and then return the new number of keychains.
  • remove_keychains() will need to be passed one int, and have a return type of int. It will ask the user for the number of keychains to remove from the order, and then return the new number of keychains.
  • view_order() will need to be passed two ints, and have a return type of void. It will display, on three different lines, the number of keychains in the order, the price per keychain, and the total cost of the order.
  • checkout() will need to be passed two ints, and have a return type of void. It will ask the user for his/her name in order to deliver them correctly, display the order information, and then thank the user, by name, for ordering.