97. Picture Menu - Link
- Task
- Code
- Question
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
Copy
1. Butterfly
2. Elephant
3. Teddy Bear
4. Snake
Which animal to draw? 1
PictureMenu.java
Copy
import java.util.Scanner;
public class PictureMenu
{
public static void main( String[] args )
{
Scanner kb = new Scanner(System.in);
int choice;
System.out.println( "1. Butterfly " );
System.out.println( "2. Elephant " );
System.out.println( "3. Teddy Bear" );
System.out.println( "4. Snake " );
System.out.print( "\nWhich animal to draw? " );
choice = kb.nextInt();
System.out.println();
if ( choice == 1 )
{
butterfly();
}
else if ( choice == 2 )
{
elephant();
}
else if ( choice == 3 )
{
// * write code here to call the function named 'teddybear'
}
else if ( choice == 4 )
{
// * write code here to call the function named 'snake'
}
else
{
System.out.println( "Sorry, that wasn't one of the choices." );
}
System.out.println( "\nGoodbye!" );
}
public static void butterfly()
{
System.out.println(" .==-. .-==. ");
System.out.println(" \\()8`-._ `. .' _.-'8()/ ");
System.out.println(" (88\" ::. \\./ .:: \"88) ");
System.out.println(" \\_.'`-::::.(#).::::-'`._/ ");
System.out.println(" `._... .q(_)p. ..._.' ");
System.out.println(" \"\"-..-'|=|`-..-\"\" ");
System.out.println(" .\"\"' .'|=|`. `\"\". ");
System.out.println(" ,':8(o)./|=|\\.(o)8:`. ");
System.out.println(" (O :8 ::/ \\_/ \\:: 8: O) ");
System.out.println(" \\O `::/ \\::' O/ ");
System.out.println(" \"\"--' `--\"\" hjw");
}
public static void elephant()
{
System.out.println(" _..--\"\"-. .-\"\"--.._ ");
System.out.println(" _.-' \\ __...----...__ / '-._");
System.out.println(" .' .:::...,' ',...:::. '.");
System.out.println("( .'``'''::; ;::'''``'. )");
System.out.println(" \\ '-) (-' /");
System.out.println(" \\ / \\ /");
System.out.println(" \\ .'.-. .-.'. /");
System.out.println(" \\ | \\0| |0/ | /");
System.out.println(" | \\ | .-==-. | / |");
System.out.println(" \\ `/`; ;`\\` /");
System.out.println(" '.._ (_ | .-==-. | _) _..'");
System.out.println(" `\"`\"-`/ `/' '\\` \\`-\"`\"`");
System.out.println(" / /`; .==. ;`\\ \\");
System.out.println(" .---./_/ \\ .==. / \\ \\");
System.out.println(" / '. `-.__) | `\"");
System.out.println(" | =(`-. '==. ;");
System.out.println(" jgs \\ '. `-. /");
System.out.println(" \\_:_) `\"--.....-'");
}
public static void teddybear()
{
System.out.println(" ___ .--. ");
System.out.println(" .--.-\" \"-' .- |");
System.out.println(" / .-,` .'");
System.out.println(" \\ ` \\");
System.out.println(" '. ! \\");
System.out.println(" | ! .--. |");
System.out.println(" \\ '--' /.____");
System.out.println(" /`-. \\__,'.' `\\");
System.out.println(" __/ \\`-.____.-' `\\ /");
System.out.println(" | `---`'-'._/-` \\----' _");
System.out.println(" |,-'` / | _.-' `\\");
System.out.println(" .' / |--'` / |");
System.out.println(" / /\\ ` | |");
System.out.println(" | .\\/ \\ .--. __ \\ |");
System.out.println(" '-' '._ / `\\ /");
System.out.println(" jgs `\\ ' |------'`");
System.out.println(" \\ | |");
System.out.println(" \\ /");
System.out.println(" '._ _.'");
System.out.println(" ``");
}
public static void snake()
{
System.out.println(" ,,'6''-,.");
System.out.println(" <====,.;;--.");
System.out.println(" _`---===. \"\"\"==__");
System.out.println(" //\"\"@@-\\===\\@@@@ \"\"\\\\");
System.out.println(" |( @@@ |===| @@@ ||");
System.out.println(" \\\\ @@ |===| @@ //");
System.out.println(" \\\\ @@ |===|@@@ //");
System.out.println(" \\\\ |===| //");
System.out.println("___________\\\\|===| //_____,----\"\"\"\"\"\"\"\"\"\"-----,_");
System.out.println(" \"\"\"\"---,__`\\===`/ _________,---------,____ `,");
System.out.println(" |==|| `\\ \\");
System.out.println(" |==| | pb ) |");
System.out.println(" |==| | _____ ______,--' '");
System.out.println(" |=| `----\"\"\" `\"\"\"\"\"\"\"\" _,-'");
System.out.println(" `=\\ __,---\"\"\"-------------\"\"\"''");
System.out.println(" \"\"\"\" ");
}
}
Assignments turned in without these things will not receive any points.
- Add the two missing function calls for menu options 3 and 4.
Answer: Add
teddybear();andsnake();in the respectiveelse ifstatements.Copyelse if ( choice == 3 ) { teddybear(); } else if ( choice == 4 ) { snake(); } - Change the code in the if statement for choice 1 so that it calls the ‘butterfly’ function twice instead of just once. What happens now when you run the program and choose option 1? (Answer in a comment right underneath where you added the extra function call.)
Answer: Add
butterfly();again in the respectiveifstatement.Copyif ( choice == 1 ) { butterfly(); butterfly(); // it will print the butterfly twice }
98. Flicker Phrase - Link
- Task
- Code
- Question
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
Copy
I pledge allegiance to the flag.
FlickerPhrase.java
Copy
import java.util.Random;
public class FlickerPhrase
{
public static void main( String[] args )
{
Random rng = new Random();
int r;
for ( int i=0; i<100000; i++ )
{
r = 1 + rng.nextInt(5);
// Write five if statements here.
// If r is 1, then call the function named 'first'.
// If r is 2, then call the function named 'second', and so on.
// Optional: after the if statements are over, add in a slight delay.
}
System.out.println("I pledge allegiance to the flag.");
}
public static void first()
{
System.out.print("I \r");
}
public static void second()
{
System.out.print(" pledge \r");
}
public static void third()
{
System.out.print(" allegiance \r");
}
public static void fourth()
{
System.out.print(" to the \r");
}
public static void fifth()
{
System.out.print(" flag.\r");
}
}
Assignments turned in without these things will not receive any points.
- Add the five if statements and function calls where indicated.
Answer: Add the following code in the
forloop.Copyif ( r == 1 ) { first(); } else if ( r == 2 ) { second(); } else if ( r == 3 ) { third(); } else if ( r == 4 ) { fourth(); } else if ( r == 5 ) { fifth(); } - (optional) Add a delay using Thread.sleep()
Answer: Add the following code after the
ifstatements.Copytry { Thread.sleep(200); } catch ( InterruptedException e ) { // do nothing }
99. Heron’s Formula - Link
- Task
- Code
- Question
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
Copy
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
HeronsFormula.java
Copy
public class HeronsFormula
{
public static void main( String[] args )
{
double a;
a = triangleArea(3, 3, 3);
System.out.println("A triangle with sides 3,3,3 has an area of " + a );
a = triangleArea(3, 4, 5);
System.out.println("A triangle with sides 3,4,5 has an area of " + a );
a = triangleArea(7, 8, 9);
System.out.println("A triangle with sides 7,8,9 has an area of " + a );
System.out.println("A triangle with sides 5,12,13 has an area of " + triangleArea(5, 12, 13) );
System.out.println("A triangle with sides 10,9,11 has an area of " + triangleArea(10, 9, 11) );
System.out.println("A triangle with sides 8,15,17 has an area of " + triangleArea(8, 15, 17) );
}
public static double triangleArea( int a, int b, int c )
{
// the code in this function computes the area of a triangle whose sides have lengths a, b, and c
double s, A;
s = (a+b+c) / 2;
A = Math.sqrt( s*(s-a)*(s-b)*(s-c) );
return A;
// ^ after computing the area, "return" it
}
}
Assignments turned in without these things will not receive any points.
- Compile and run both files (HeronsFormula.java and HeronsFormulaNoFunction.java). Do they both produce the same output? (Answer in a comment in HeronsFormula.java.)
Answer: Yes, they both produce the same output.
- How many lines long is HeronsFormulaNoFunction.java? How many lines long is HeronsFormula.java if you don’t count the two lines of comments inside the
triangleArea()function? (Put the answers to both questions in a comment in HeronsFormula.java.)Answer: HeronsFormulaNoFunction.java is 50 lines long. HeronsFormula.java is 32 lines long.
- There is a bug in the formula for both files. When
(a+b+c)is an odd number, dividing by2throws away the.5. Fix both files so that instead of(a+b+c) / 2you have(a+b+c) / 2.0everywhere it occurs. Was it easier to fix the file that used a function, or the one that didn’t use a function? Answer in a comment.Answer: It was easier to fix the file that used a function.
HeronsFormula.javaCopys = (a+b+c) / 2.0;HeronsFormulaNoFunction.javaCopys = (a+b+c) / 2.0; - Add one more test to both files: find the area of a triangle with sides 9, 9, and 9. Was it difficult to add to the file that used a function? Answer in a comment on the line below where you added the new function call.
Answer: It was not difficult to add to the file that used a function.
HeronsFormula.javaCopySystem.out.println("A triangle with sides 9,9,9 has an area of " + triangleArea(9, 9, 9) ); - (You don’t need to turn in HeronsFormulaNoFunction.java. Only turn in one file: HeronsFormula.java)
100. Distance Formula - Link
- Task
- Code
- Answer
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:
You must name the function
You must name the function distance, and it must return a double giving the distance between the two points.Files NeededSample OutputCopy
(-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
DistanceFormula.java
Copy
public class DistanceFormula
{
public static void main( String[] args )
{
// test the formula a bit
double d1 = distance(-2,1 , 1,5);
System.out.println(" (-2,1) to (1,5) => " + d1 );
double d2 = distance(-2,-3 , -4,4);
System.out.println(" (-2,-3) to (-4,4) => " + d2 );
System.out.println(" (2,-3) to (-1,-2) => " + distance(2,-3,-1,-2) );
System.out.println(" (4,5) to (4,5) => " + distance(4,5,4,5) );
}
public static double distance( int x1, int y1, int x2, int y2 )
{
// put your code up in here
}
}
DistanceFormula.java
Copy
public static double distance( int x1, int y1, int x2, int y2 )
{
return Math.sqrt( Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) );
}
101. Month Name - Link
- Task
- Code
- Answer
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.
The function must be called
| Number | Month |
|---|---|
| 1 | January |
| 2 | February |
| 3 | March |
| 4 | April |
| 5 | May |
| 6 | June |
| 7 | July |
| 8 | August |
| 9 | September |
| 10 | October |
| 11 | November |
| 12 | December |
| anything else | error |
month_name(), and must have one parameter (an integer), and return a String.And finally, here’s some starter code.Files NeededSample OutputCopy
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
MonthName.java
Copy
public class MonthName
{
public static String month_name( int month )
{
String result;
// Your code goes in here.
return result;
}
public static void main( String[] args )
{
System.out.println( "Month 1: " + month_name(1) );
System.out.println( "Month 2: " + month_name(2) );
System.out.println( "Month 3: " + month_name(3) );
System.out.println( "Month 4: " + month_name(4) );
System.out.println( "Month 5: " + month_name(5) );
System.out.println( "Month 6: " + month_name(6) );
System.out.println( "Month 7: " + month_name(7) );
System.out.println( "Month 8: " + month_name(8) );
System.out.println( "Month 9: " + month_name(9) );
System.out.println( "Month 10: " + month_name(10) );
System.out.println( "Month 11: " + month_name(11) );
System.out.println( "Month 12: " + month_name(12) );
System.out.println( "Month 43: " + month_name(43) );
}
}
MonthName.java
Copy
public static String month_name( int month )
{
String result;
if ( month == 1 )
{
result = "January";
}
else if ( month == 2 )
{
result = "February";
}
else if ( month == 3 )
{
result = "March";
}
else if ( month == 4 )
{
result = "April";
}
else if ( month == 5 )
{
result = "May";
}
else if ( month == 6 )
{
result = "June";
}
else if ( month == 7 )
{
result = "July";
}
else if ( month == 8 )
{
result = "August";
}
else if ( month == 9 )
{
result = "September";
}
else if ( month == 10 )
{
result = "October";
}
else if ( month == 11 )
{
result = "November";
}
else if ( month == 12 )
{
result = "December";
}
else
{
result = "error";
}
return result;
}
102. Month Offset - Link
- Task
- Code
- Answer
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:
Of course, here’s the main() to test your function.Files NeededSample Output
| Month | Month Offset |
|---|---|
| 1 | 1 |
| 2 | 4 |
| 3 | 4 |
| 4 | 0 |
| 5 | 2 |
| 6 | 5 |
| 7 | 0 |
| 8 | 3 |
| 9 | 6 |
| 10 | 1 |
| 11 | 4 |
| 12 | 6 |
| anything else | -1 |
Copy
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
MonthOffset.java
Copy
public class MonthOffset
{
public static int month_offset( int month )
{
int result;
// Your code goes in here.
return result;
}
public static void main( String[] args )
{
System.out.println( "Offset for month 1: " + month_offset(1) );
System.out.println( "Offset for month 2: " + month_offset(2) );
System.out.println( "Offset for month 3: " + month_offset(3) );
System.out.println( "Offset for month 4: " + month_offset(4) );
System.out.println( "Offset for month 5: " + month_offset(5) );
System.out.println( "Offset for month 6: " + month_offset(6) );
System.out.println( "Offset for month 7: " + month_offset(7) );
System.out.println( "Offset for month 8: " + month_offset(8) );
System.out.println( "Offset for month 9: " + month_offset(9) );
System.out.println( "Offset for month 10: " + month_offset(10) );
System.out.println( "Offset for month 11: " + month_offset(11) );
System.out.println( "Offset for month 12: " + month_offset(12) );
System.out.println( "Offset for month 43: " + month_offset(43) );
}
}
MonthOffset.java
Copy
public static int month_offset( int month )
{
int result;
if ( month == 1 )
{
result = 1;
}
else if ( month == 2 )
{
result = 4;
}
else if ( month == 3 )
{
result = 4;
}
else if ( month == 4 )
{
result = 0;
}
else if ( month == 5 )
{
result = 2;
}
else if ( month == 6 )
{
result = 5;
}
else if ( month == 7 )
{
result = 0;
}
else if ( month == 8 )
{
result = 3;
}
else if ( month == 9 )
{
result = 6;
}
else if ( month == 10 )
{
result = 1;
}
else if ( month == 11 )
{
result = 4;
}
else if ( month == 12 )
{
result = 6;
}
else
{
result = -1;
}
return result;
}
103. Weekday Calculator - Link
- Task
- Code
- Answer
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.
- Find the number of years since 1900, and put it into a variable called
yy. Simply subtract 1900 from the year to get this. - 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 intotal. - Also add the number of years since 1900 to
total. - Add the day of the month to
total. - Using the function
month_offset()you wrote, find the “month offset” and add it tototal. - 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 functionis_leap()provided to determine if the year is a leap year. - Find the remainder when
totalis divided by 7. Pass this remainder to the functionweekday_name()you wrote to determine the day of the week the person was born. - 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. - Return that String value.
Copy
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!
WeekdayCalculator.java
Copy
import java.util.Scanner;
public class WeekdayCalculator
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to Mr. Mitchell's fantastic birth-o-meter!");
System.out.println();
System.out.println("All you have to do is enter your birth date, and it will");
System.out.println("tell you the day of the week on which you were born.");
System.out.println();
System.out.println("Some automatic tests....");
System.out.println("12 10 2003 => " + weekday(12,10,2003));
System.out.println(" 2 13 1976 => " + weekday(2,13,1976));
System.out.println(" 2 13 1977 => " + weekday(2,13,1977));
System.out.println(" 7 2 1974 => " + weekday(7,2,1974));
System.out.println(" 1 15 2003 => " + weekday(1,15,2003));
System.out.println("10 13 2000 => " + weekday(10,13,2000));
System.out.println();
System.out.println("Now it's your turn! What's your birthday?");
System.out.print("Birth date (mm dd yyyy): ");
int mm = keyboard.nextInt();
int dd = keyboard.nextInt();
int yyyy = keyboard.nextInt();
// put a function call for weekday() here
System.out.println("You were born on ");
}
public static String weekday( int mm, int dd, int yyyy )
{
int yy, total;
String date = "";
// bunch of calculations go here
date = month_name(mm) + ", " + yyyy;
return date;
}
// paste your functions from MonthName, WeekdayName, and MonthOffset here
public static boolean is_leap( int year )
{
// years which are evenly divisible by 4 are leap years,
// but years divisible by 100 are not leap years,
// though years divisible by 400 are leap years
boolean result;
if ( year%400 == 0 )
result = true;
else if ( year%100 == 0 )
result = false;
else if ( year%4 == 0 )
result = true;
else
result = false;
return result;
}
}
WeekdayCalculator.java
Copy
public static String weekday( int mm, int dd, int yyyy )
{
int yy, total;
String date = "";
yy = yyyy - 1900;
total = yy / 4;
total += yy;
total += dd;
total += month_offset(mm);
if ( is_leap(yyyy) && (mm == 1 || mm == 2) )
{
total -= 1;
}
date = weekday_name(total % 7) + ", " + month_name(mm) + " " + dd + ", " + yyyy;
return date;
}
public static String weekday_name( int weekday )
{
String result = "";
if ( weekday == 1 )
{
result = "Sunday";
}
else if ( weekday == 2 )
{
result = "Monday";
}
else if ( weekday == 3 )
{
result = "Tuesday";
}
else if ( weekday == 4 )
{
result = "Wednesday";
}
else if ( weekday == 5 )
{
result = "Thursday";
}
else if ( weekday == 6 )
{
result = "Friday";
}
else if ( weekday == 0 )
{
result = "Saturday";
}
else
{
result = "error";
}
return result;
}
public static int month_offset( int month )
{
int result;
if ( month == 1 )
{
result = 1;
}
else if ( month == 2 )
{
result = 4;
}
else if ( month == 3 )
{
result = 4;
}
else if ( month == 4 )
{
result = 0;
}
else if ( month == 5 )
{
result = 2;
}
else if ( month == 6 )
{
result = 5;
}
else if ( month == 7 )
{
result = 0;
}
else if ( month == 8 )
{
result = 3;
}
else if ( month == 9 )
{
result = 6;
}
else if ( month == 10 )
{
result = 1;
}
else if ( month == 11 )
{
result = 4;
}
else if ( month == 12 )
{
result = 6;
}
else
{
result = -1;
}
return result;
}
public static String month_name( int month )
{
String result;
if ( month == 1 )
{
result = "January";
}
else if ( month == 2 )
{
result = "February";
}
else if ( month == 3 )
{
result = "March";
}
else if ( month == 4 )
{
result = "April";
}
else if ( month == 5 )
{
result = "May";
}
else if ( month == 6 )
{
result = "June";
}
else if ( month == 7 )
{
result = "July";
}
else if ( month == 8 )
{
result = "August";
}
else if ( month == 9 )
{
result = "September";
}
else if ( month == 10 )
{
result = "October";
}
else if ( month == 11 )
{
result = "November";
}
else if ( month == 12 )
{
result = "December";
}
else
{
result = "error";
}
return result;
}
public static boolean is_leap( int year )
{
boolean result;
if ( year%400 == 0 )
result = true;
else if ( year%100 == 0 )
result = false;
else if ( year%4 == 0 )
result = true;
else
result = false;
return result;
}
104. Area Calculator - Link
- Task
- Code
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: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
You’ll need the value of π for
Copy
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
main(), and values must be passed to the functions and returned from them.| Shape | Formula |
|---|---|
| square | A=s2 |
| rectangle | A=l×w |
| triangle | A=21×bh |
| circle | A=πr2 |
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 OutputCopy
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.
AreaCalculator.java
Copy
import java.util.Scanner;
public class AreaCalculator
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Shape Area Calculator version 0.1 (c) 2005 Mitchell Sample Output, Inc.");
int choice;
do
{
System.out.println("\n-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
System.out.println("1) Triangle");
System.out.println("2) Rectangle");
System.out.println("3) Square");
System.out.println("4) Circle");
System.out.println("5) Quit");
System.out.print("Which shape: ");
choice = keyboard.nextInt();
if ( choice == 1 )
{
System.out.print("\nBase: ");
int base = keyboard.nextInt();
System.out.print("Height: ");
int height = keyboard.nextInt();
System.out.printf("\nThe area is %s.\n", area_triangle(base, height));
}
else if ( choice == 2 )
{
System.out.print("\nLength: ");
int length = keyboard.nextInt();
System.out.print("Width: ");
int width = keyboard.nextInt();
System.out.printf("\nThe area is %s.\n", area_rectangle(length, width));
}
else if ( choice == 3 )
{
System.out.print("\nSide length: ");
int side = keyboard.nextInt();
System.out.printf("\nThe area is %s.\n", area_square(side));
}
else if ( choice == 4 )
{
System.out.print("\nRadius: ");
int radius = keyboard.nextInt();
System.out.printf("\nThe area is %.4s.\n", area_circle(radius));
}
} while ( choice != 5 );
System.out.println("\nGoodbye.");
}
public static double area_circle( int radius )
{
return Math.PI * Math.pow(radius, 2);
}
public static int area_rectangle( int length, int width )
{
return length * width;
}
public static int area_square( int side )
{
return (int) Math.pow(side, 2);
}
public static int area_triangle( int base, int height )
{
return (int) (0.5 * base * height);
}
}
105. Function Call Alphabet - Link
- Task
- Code
- Answer
Download and finish the following code to practice writing function calls.Files NeededSample Output
Copy
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:
FunctionCallAlphabet.java
Copy
// Function Call Alphabet - Yet another program to practice functions, but this time it's function calls only.
public class FunctionCallAlphabet
{
public static void main( String[] args )
{
// ???? a( ???? ); // displays a word beginning with A
// ???? b( ???? ); // returns the word to be displayed
// ???? c( ???? ); // pass it 'true' and it will display a word beginning with C
// ???? d( ???? ); // displays a word beginning with D
// ???? e( ???? ); // pass it the number of letters to display (9)
// ???? f( ???? ); // displays the word you pass it beginning with "F"
// ???? g( ???? ); // returns the word to be displayed
// ???? h( ???? ); // tell it how many times to display the word (1)
System.out.println();
// ???? i( ???? ); // pass it any integer and it will display a word beginning with I
// ???? j( ???? ); // returns a different word depending on what you pass it (1-3)
// ???? k( ???? ); // displays a word beginning with K
// ???? l( ???? ); // displays a different word depending on the two booleans you pass it
// ???? m( ???? ); // displays a different word depending on the two booleans you pass it
// ???? n( ???? ); // displays the word you pass it beginning with "N"
// ???? o( ???? ); // displays a word beginning with O and returns a useless value
// ???? p( ???? ); // returns the word to be displayed
// ???? q( ???? ); // displays the word
System.out.println();
// ???? r( ???? ); // returns a different word depending on if you pass it 'true' or 'false'
// ???? s( ???? ); // pass it the number of letters to display (6)
// ???? t( ???? ); // displays the word you pass it beginning with "T"
// ???? u( ???? ); // returns the word to be displayed
// ???? v( ???? ); // tell it how many times to display the word (1)
// ???? w( ???? ); // pass it any integer and it will display a word beginning with W
// ???? x( ???? ); // returns a different word depending on what you pass it (1-2)
// ???? y( ???? ); // displays a word beginning with Y
// ???? z( ???? ); // returns a different word depending on which two boolean values you pass it
System.out.println();
}
/**************************************
* Don't change anything below here!! *
*************************************/
public static void a()
{
System.out.print("Ant ");
}
public static String b()
{
return "Banana ";
}
public static void c(boolean doit)
{
if ( doit )
System.out.print("Crocodile ");
}
public static void d()
{
System.out.print("Doggie ");
}
public static void e(int howmany)
{
String s;
s = "Elephant ";
int x = 0;
while ( x < howmany )
{
System.out.print( s.charAt(x) );
x = x+1;
}
}
public static void f(String word)
{
System.out.print(word + " ");
}
public static String g()
{
return "Gorilla ";
}
public static void h(int reps)
{
int x = 0;
while ( x < reps )
{
System.out.print("Horseradish ");
x = x+1;
}
}
public static void i(int ignoredparameter)
{
ignoredparameter = 32;
String space = Character.toString( (char) ignoredparameter );
System.out.print("Ice_cream" + space);
}
public static String j(int whichone)
{
if ( whichone == 1 )
return "Jambalaya ";
else if ( whichone == 2 )
return "Juniper ";
else
return "Jackrabbit ";
}
public static void k()
{
// the bird OR the fruit
System.out.print("Kiwi ");
}
public static void l(boolean a, boolean b)
{
if ( a && b )
System.out.print("Lettuce ");
else
System.out.print("Lhasa_apso ");
}
public static void m(boolean a, boolean b)
{
if ( a || b )
System.out.print("Mango ");
else
System.out.print("Monkey! ");
}
public static void n(String word)
{
System.out.print(word + " ");
}
public static int o()
{
System.out.print("Orangutan ");
return 1; // just for kicks; the return value doesn't mean anything
}
public static String p()
{
return "Parrot ";
}
public static void q()
{
System.out.print("Quail ");
}
public static String r(boolean first)
{
if ( first )
return "Rabbit ";
else
return "Radish ";
}
public static void s(int howmany)
{
String s;
s = "Snake ";
int x = 0;
while ( x < howmany )
{
System.out.print( s.charAt(x) );
x = x+1;
}
}
public static void t(String word)
{
System.out.print(word + " ");
}
public static String u()
{
return "Ugli_fruit ";
}
public static void v(int reps)
{
int x = 0;
while ( x < reps )
{
System.out.print("Valentine_candy ");
x = x+1;
}
}
public static void w(int ignoredparameter)
{
ignoredparameter = 32;
String space = Character.toString( (char) ignoredparameter );
System.out.print("Walrus" + space);
}
public static String x(int whichone)
{
if ( whichone == 1 )
return "X_files ";
else
return "X_men ";
}
public static void y()
{
System.out.print("Yams ");
}
public static String z(boolean a, boolean b)
{
if ( a || b )
return "Zanahorias ";
else
return "Zebra ";
}
}
FunctionCallAlphabet.java
Copy
public static void main( String[] args )
{
a();
System.out.print( b() );
c(true);
d();
e(9);
f("Frog ");
System.out.print( g() );
h(1);
System.out.println();
i(32);
System.out.print( j(3) );
k();
l(true, false);
m(false, false);
n("Narwhal ");
o();
System.out.print( p() );
q();
System.out.println();
System.out.print( r(true) );
s(6);
t("Thyme ");
System.out.print( u() );
v(1);
w(32);
System.out.print( x(2) );
y();
System.out.print( z(true, false) );
System.out.println();
}
106. Fill-In Functions - Link
- Task
- Code
- Answer
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
Copy
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.
FillInFunctions.java
Copy
// Fill-In Functions - Fix the broken functions and function calls.
public class FillInFunctions
{
public static void main( String[] args )
{
// Fill in the function calls where appropriate.
System.out.println("Watch as we demonstrate functions.");
System.out.println();
System.out.println("I'm going to get a random character from A-Z");
char c = '!';
// randchar();
System.out.println("The character is: " + c );
System.out.println();
System.out.println("Now let's count from -10 to 10");
int begin, end;
begin = -10;
end = 10;
// counter();
System.out.println("How was that?");
System.out.println();
System.out.println("Now we take the absolute value of a number.");
int x, y = 99;
x = -10;
// abso();
System.out.println("|" + x + "| = " + y );
System.out.println();
System.out.println("That's all. This program has been brought to you by:");
// credits();
}
/*
public static ???? credits( ???? )
// No parameters.
{
// displays some boilerplate text saying who wrote this program, etc.
System.out.println();
System.out.println("programmed by Graham Mitchell");
System.out.println("modified by [your name here]");
System.out.print("This code is distributed under the terms of the standard ");
System.out.println("BSD license. Do with it as you wish.");
return ??;
}
*/
/*
public static ???? randchar( ???? )
// No parameters.
{
// chooses a random character in the range "A" to "Z"
int numval;
char charval;
// pick a random number from 0 to 25
numval = (int)(Math.random()*26);
// now add that offset to the value of the letter 'A'
charval = (char) ('A' + numval);
return ??;
}
*/
/*
public static ???? counter( ???? )
// Parameters are:
// int start;
// int stop;
{
// counts from start to stop by ones
int ctr;
ctr = start;
while ( ctr <= stop )
{
System.out.print(ctr + " ");
ctr = ctr+1;
}
return ??;
}
*/
/*
public static ???? abso( ???? )
// Parameters are:
// int value;
{
// finds the absolute value of the parameter
int absval;
if ( value < 0 )
absval = -value;
else
absval = value;
return ??;
}
*/
}
FillInFunctions.java
Copy
public static void main( String[] args )
{
// Fill in the function calls where appropriate.
System.out.println("Watch as we demonstrate functions.");
System.out.println();
System.out.println("I'm going to get a random character from A-Z");
char c = '!';
c = randchar();
System.out.println("The character is: " + c );
System.out.println();
System.out.println("Now let's count from -10 to 10");
int begin, end;
begin = -10;
end = 10;
counter(begin, end);
System.out.println("How was that?");
System.out.println();
System.out.println("Now we take the absolute value of a number.");
int x, y = 99;
x = -10;
y = abso(x);
System.out.println("|" + x + "| = " + y );
System.out.println();
System.out.println("That's all. This program has been brought to you by:");
credits();
}
public static void credits()
// No parameters.
{
// displays some boilerplate text saying who wrote this program, etc.
System.out.println();
System.out.println("programmed by Graham Mitchell");
System.out.println("modified by [your name here]");
System.out.print("This code is distributed under the terms of the standard ");
System.out.println("BSD license. Do with it as you wish.");
}
public static char randchar()
// No parameters.
{
// chooses a random character in the range "A" to "Z"
int numval;
char charval;
// pick a random number from 0 to 25
numval = (int)(Math.random()*26);
// now add that offset to the value of the letter 'A'
charval = (char) ('A' + numval);
return charval;
}
public static void counter( int start, int stop )
// Parameters are:
// int start;
// int stop;
{
// counts from start to stop by ones
int ctr;
ctr = start;
while ( ctr <= stop )
{
System.out.print(ctr + " ");
ctr = ctr+1;
}
}
public static int abso( int value )
// Parameters are:
// int value;
{
// finds the absolute value of the parameter
int absval;
if ( value < 0 )
absval = -value;
else
absval = value;
return absval;
}
107. More Fill-In Functions - Link
- Task
- Code
- Answer
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
Copy
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
MoreFillInFunctions.java
Copy
// More Fill-In Functions - Fix the broken functions and function calls (again).
import java.util.Scanner;
public class MoreFillInFunctions
{
public static void main( String[] args )
{
// Fill in the function calls where indicated.
System.out.println("Here we go.");
System.out.println();
System.out.println("Some random numbers, if you please: ");
int lo, hi, val1 = 999, val2 = 999;
lo = 1;
hi = 10;
// superrand();
System.out.println("First: " + val1 );
// superrand(); // this time, put hi first
System.out.println("Second: " + val2 );
if ( val1 == val2 )
System.out.println("Hey! They were the same!");
else
System.out.println("They were not the same.");
System.out.println();
System.out.print("More counting, but this time not by ones: ");
// count from 2 to 8 by 2s
// stepcount();
// count from 10 to 2 by 2s
// stepcount();
System.out.println();
System.out.println("Let's figure a project grade.");
int a=4,b=3,c=4,d=5,e=2,f=1, result=999;
// project_grade();
System.out.println("434521 -> " + result );
System.out.println();
System.out.print("Finally, some easy ones.");
String nombre = "ERROR";
// get_name();
System.out.println("Hi, " + nombre );
System.out.println();
System.out.println("Do you feel lucky, punk?");
// crash();
System.out.println();
}
/*
public static ???? superrand( ???? )
// Parameters are:
// int a;
// int b;
{
// picks a number between a and b, no matter which is larger
int c;
if ( a < b ) // b is larger
c = a + (int)(Math.random()*(b-a+1));
else if ( a > b ) // a is larger
c = b + (int)(Math.random()*(a-b+1));
else
c = a; // or c = b; doesn't matter since they're equal
return ??;
}
*/
/*
public static ???? stepcount( ???? )
// Parameters are:
// int first;
// int last;
// int step;
{
// counts from 'first' to 'last' by 'step'
// handles forward and backward
int x;
if ( first < last )
{
x = first;
while ( x <= last )
{
System.out.print(x + " ");
x = x + step;
}
}
else
{
x = first;
while ( x >= last )
{
System.out.print(x + " ");
x = x - step;
}
}
return ??;
}
*/
/*
public static ???? project_grade( ???? )
// Parameters are:
// int p1, p2, p3, p4, p5, p6;
{
// given six integers representing scores out of five in each of the
// six categories for the first six weeks project: tells you your
// overall project grade out of 100
int overall_grade;
overall_grade = p1 * 6; // six points per point for the first category
overall_grade = overall_grade + ( p2 * 6 ); // six points per point
overall_grade = overall_grade + ( p3 * 4 ); // four points per point
overall_grade = overall_grade + ( p4 * 2 ); // two points per point
overall_grade = overall_grade + ( p5 * 1 ); // one point per point
overall_grade = overall_grade + ( p6 * 1 ); // one point per point
return ??;
}
*/
/*
public static ???? get_name( ???? )
// No parameters.
{
// finds out the user's name
Scanner keyboard = new Scanner(System.in);
String name;
System.out.print("Please enter your name: ");
name = keyboard.next();
return ??;
}
*/
/*
public static ???? crash( ???? )
// No parameters.
{
// displays "you win" or "you lose". You lose 90% of the time.
String magic_word;
if ( (int)(Math.random()*10) == 0 )
{
// What do you know? We won!
magic_word = "win";
}
else
{
// No big suprise; we lost.
magic_word = "lose";
}
System.out.print("you " + magic_word);
return ??;
}
*/
}
MoreFillInFunctions.java
Copy
public static void main( String[] args )
{
// Fill in the function calls where indicated.
System.out.println("Here we go.");
System.out.println();
System.out.println("Some random numbers, if you please: ");
int lo, hi, val1 = 999, val2 = 999;
lo = 1;
hi = 10;
val1 = superrand(lo, hi);
System.out.println("First: " + val1 );
val2 = superrand(hi, lo);
System.out.println("Second: " + val2 );
if ( val1 == val2 )
System.out.println("Hey! They were the same!");
else
System.out.println("They were not the same.");
System.out.println();
System.out.print("More counting, but this time not by ones: ");
// count from 2 to 8 by 2s
stepcount(2, 8, 2);
// count from 10 to 2 by 2s
stepcount(10, 2, 2);
System.out.println();
System.out.println("Let's figure a project grade.");
int a=4,b=3,c=4,d=5,e=2,f=1, result=999;
result = project_grade(a, b, c, d, e, f);
System.out.println("434521 -> " + result );
System.out.println();
System.out.print("Finally, some easy ones.");
String nombre = "ERROR";
nombre = get_name();
System.out.println("Hi, " + nombre );
System.out.println();
System.out.println("Do you feel lucky, punk?");
crash();
System.out.println();
}
public static int superrand( int a, int b )
// Parameters are:
// int a;
// int b;
{
// picks a number between a and b, no matter which is larger
int c;
if ( a < b ) // b is larger
c = a + (int)(Math.random()*(b-a+1));
else if ( a > b ) // a is larger
c = b + (int)(Math.random()*(a-b+1));
else
c = a; // or c = b; doesn't matter since they're equal
return c;
}
public static void stepcount( int first, int last, int step )
// Parameters are:
// int first;
// int last;
// int step;
{
// counts from 'first' to 'last' by 'step'
// handles forward and backward
int x;
if ( first < last )
{
x = first;
while ( x <= last )
{
System.out.print(x + " ");
x = x + step;
}
}
else
{
x = first;
while ( x >= last )
{
System.out.print(x + " ");
x = x - step;
}
}
}
public static int project_grade( int p1, int p2, int p3, int p4, int p5, int p6 )
// Parameters are:
// int p1, p2, p3, p4, p5, p6;
{
// given six integers representing scores out of five in each of the
// six categories for the first six weeks project: tells you your
// overall project grade out of 100
int overall_grade;
overall_grade = p1 * 6; // six points per point for the first category
overall_grade = overall_grade + ( p2 * 6 ); // six points per point
overall_grade = overall_grade + ( p3 * 4 ); // four points per point
overall_grade = overall_grade + ( p4 * 2 ); // two points per point
overall_grade = overall_grade + ( p5 * 1 ); // one point per point
overall_grade = overall_grade + ( p6 * 1 ); // one point per point
return overall_grade;
}
public static String get_name()
// No parameters.
{
// finds out the user's name
Scanner keyboard = new Scanner(System.in);
String name;
System.out.print("Please enter your name: ");
name = keyboard.next();
return name;
}
public static void crash()
// No parameters.
{
// displays "you win" or "you lose". You lose 90% of the time.
String magic_word;
if ( (int)(Math.random()*10) == 0 )
{
// What do you know? We won!
magic_word = "win";
}
else
{
// No big suprise; we lost.
magic_word = "lose";
}
System.out.print("you " + magic_word);
}
108. Keychains for Sale - Link
- Task
- Code
Write a program that pulls up a menu with 4 options. It should look something like…Sample Output
Copy
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.
Keychains1.java
Copy
import java.util.Scanner;
public class Keychains1
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
int choice;
System.out.println("Ye Olde Keychain Shoppe\n");
do
{
System.out.println("1. Add Keychains to Order");
System.out.println("2. Remove Keychains from Order");
System.out.println("3. View Current Order");
System.out.println("4. Checkout\n");
System.out.print("Please enter your choice: ");
choice = keyboard.nextInt();
if ( choice == 1 )
{
add_keychains();
}
else if ( choice == 2 )
{
remove_keychains();
}
else if ( choice == 3 )
{
view_order();
}
else if ( choice == 4 )
{
checkout();
}
else
{
System.out.println("Invalid choice.");
}
} while ( choice != 4 );
}
public static void add_keychains()
{
System.out.println("\nADD KEYCHAINS\n");
}
public static void remove_keychains()
{
System.out.println("\nREMOVE KEYCHAINS\n");
}
public static void view_order()
{
System.out.println("\nVIEW ORDER\n");
}
public static void checkout()
{
System.out.println("\nCHECKOUT\n");
}
}
109. Keychains for Sale, for real this time - Link
- Task
- Code
Okay, now it is time to make the keychain shop actually work.Sample Output
Copy
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.
Keychains2.java
Copy
import java.util.Scanner;
public class Keychains2
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
int choice;
int keychains = 0;
int price = 10;
System.out.println("Ye Olde Keychain Shoppe\n");
do
{
System.out.println("1. Add Keychains to Order");
System.out.println("2. Remove Keychains from Order");
System.out.println("3. View Current Order");
System.out.println("4. Checkout\n");
System.out.print("Please enter your choice: ");
choice = keyboard.nextInt();
if ( choice == 1 )
{
keychains = add_keychains(keychains);
}
else if ( choice == 2 )
{
keychains = remove_keychains(keychains);
}
else if ( choice == 3 )
{
view_order(keychains, price);
}
else if ( choice == 4 )
{
checkout(keychains, price);
}
else
{
System.out.println("Invalid choice.");
}
} while ( choice != 4 );
}
public static int add_keychains( int keychains )
{
Scanner keyboard = new Scanner(System.in);
int add;
System.out.printf("\nYou have %s keychains. How many to add? ", keychains);
add = keyboard.nextInt();
keychains += add;
System.out.printf("You now have %s keychains.\n\n", keychains);
return keychains;
}
public static int remove_keychains( int keychains )
{
Scanner keyboard = new Scanner(System.in);
int remove;
System.out.printf("\nYou have %s keychains. How many to remove? ", keychains);
remove = keyboard.nextInt();
keychains -= remove;
System.out.printf("You now have %s keychains.\n\n", keychains);
return keychains;
}
public static void view_order( int keychains, int price )
{
System.out.printf("\nYou have %s keychains.\n", keychains);
System.out.printf("Keychains cost $%s each.\n", price);
System.out.printf("Total cost is $%s.\n\n", keychains * price);
}
public static void checkout( int keychains, int price )
{
Scanner keyboard = new Scanner(System.in);
String name;
System.out.print("\nCHECKOUT\n\n");
System.out.print("What is your name? ");
name = keyboard.next();
System.out.printf("You have %s keychains.\n", keychains);
System.out.printf("Keychains cost $%s each.\n", price);
System.out.printf("Total cost is $%s.\n", keychains * price);
System.out.printf("Thanks for your order, %s!\n", name);
}
}