Skip to content

Instantly share code, notes, and snippets.

@edisplay
Created August 28, 2016 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edisplay/d74a05f1fa7a414ed77831ff1464a564 to your computer and use it in GitHub Desktop.
Save edisplay/d74a05f1fa7a414ed77831ff1464a564 to your computer and use it in GitHub Desktop.
https://repl.it/CxGq/24 created by edisplay
// Week 1 Homework - Francisco Cruz 3309
import java.util.*;
class Main {
public static void main(String[] args) {
/*
Q1. add-function
Write a method named add that takes in two numbers as arguments. The function should return the sum of the two numbers.
Examples:
add(1,2); // returns 3
add(10,12); // returns 22
*/
add(10, 12);
/*
Q2. age-calculator
Write a method named calculateAge that takes two arguments: birth year and current year. The method should then calculate the two possible ages based on that year, and return the result in the following format: (replacing 'NN' with the possible years) : "You are either NN or NN."
Examples:
calculateAge(1987, 2016); // returns 'You are either 28 or 29.'
*/
calculateAge(1987, 2016);
/*
Q3. exes-and-ohs
Check to see if a string has the same count of 'x's and 'o's. The method must return a boolean and be case insensitive. The string may contain any Unicode character -- not just 'x's and 'o's -- and may be of any length.
Example outputs:
XO("ooxx") // returns true
XO("xooxx") // returns false
XO("ooxXm") // returns true
XO("zpzpzpp") // returns true because zero 'x's and 'o's are present
XO("zzoo") // returns false
*/
String XO = "abc";
int O = 0;
int X = 0;
for( int i = 0; i < XO.length(); i++ ) {
if( XO.charAt(i) == 'o' ) {
O++;
} else if ( XO.charAt(i) == 'x' ) {
X++;
}
}
if ( O == X ) {
System.out.println("They have the same count of 'x's and 'o's");
} else {
System.out.println("They don't have the same count of 'x's and 'o's");
}
/*
Q4. endsly
Write a Java method that takes a string as a parameter and returns true if it ends in "ly".
*/
endsly();
/*
Q5. chessboard
Write a program that creates a single string that represents an 8×8 grid, using newline characters to separate lines. At each position of the grid there is either a space or a “#” character. The characters should form a chess board. Printing the string should show something like this:
```
# # # #
# # # #
# # # #
# # #
# # # #
# # # #
# # # #
# # # #
```
When you have a program that generates this pattern, define a variable size = 8 and change the program so that it works for any size, outputting a grid of the given width and height.
*/
String gridChar = "#";
String gridSpace = " ";
String gridNewLine = "\n";
int gridCount = 0;
int gridValue = 8;
int finalGridValue = 4;
// for (int finalChessIndex = 0; finalChessIndex < finalGridValue; finalChessIndex++) {
// if ( gridCount % 8 == 0 ) {
// for (int chessIndex = 0; chessIndex < gridValue; chessIndex++) {
// gridCount++;
// if ( gridCount % 2 == 0 ) {
// System.out.print(gridChar);
// } else if ( gridCount % 2 == 1 ) {
// System.out.print(gridSpace);
// }
// }
// }
// System.out.print(gridNewLine);
// }
for (int fci = 0; fci < finalGridValue; fci++) {
for (int finalChessIndex = 0; finalChessIndex < 1; finalChessIndex++) {
if ( gridCount % 8 == 0 ) {
for (int chessIndex = 0; chessIndex < gridValue; chessIndex++) {
gridCount++;
if ( gridCount % 2 == 0 ) {
System.out.print(gridChar);
} else if ( gridCount % 2 == 1 ) {
System.out.print(gridSpace);
}
}
}
System.out.print(gridNewLine);
}
for (int finalChessIndex = 0; finalChessIndex < 1; finalChessIndex++) {
if ( gridCount % 8 == 0 ) {
for (int chessIndex = 0; chessIndex < gridValue; chessIndex++) {
gridCount++;
if ( gridCount % 2 == 0 ) {
System.out.print(gridSpace);
} else if ( gridCount % 2 == 1 ) {
System.out.print(gridChar);
}
}
}
System.out.print(gridNewLine);
}
}
/*
Q6. scanner-hungry-hippos
Hippos only like to eat foods that begin with the letter 'h'. In an effort to reduce food waste, the local zoo has hired you to write a Java program that can predict whether or not the hippos will eat a given food.
While running, your program should prompt the user to enter a food. If the food is one that hippos like to eat, the program should print "Yum!" -- otherwise, it should print "Yuck!".
For example:
Enter a food:
hot dogs
Yum!
Enter a food:
HAMBURGERS
Yum!
Enter a food:
frozen yogurt
Yuck!
*/
String hippoFood;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a food for the Hippos to eat: ");
hippoFood = scanner.nextLine();
// System.out.print(hippoFood);
if (hippoFood.charAt(0) == 'h' || hippoFood.charAt(0) == 'H') {
System.out.print("Yum!");
} else {
System.out.print("Yuck!");
}
System.out.println("\n");
/*
Q7. string-elide
Write a method that takes a string parameter. For longer strings, the method returns a new string constructed out of the first three letters of the argument, followed by three periods ("..."), followed by the last letter of the argument.
However, if the resulting string is not shorter than the argument, the method should return the argument instead.
For example,
elide("Hello!") // returns "Hello!"
elide("Hello, world!") // returns "Hel...!"
elide("That's not my name.") // returns "Tha...."
Remember that String.substring() can take two arguments: the start index and the end index.
*/
elide("That's not my name.");
/*
Q8. triangle
Write a loop that will print the following triangle to the console:
#
##
###
####
#####
######
#######
In progress ... */
String hashForTriangle = "#";
for (int trIndex = 0; trIndex < 8; trIndex++) {
for (int trIndex2 = 0; trIndex2 < trIndex; trIndex2++) {
System.out.println(hashForTriangle);
}
System.out.println("");
}
/*
*/
/*
*/
/*
*/
/*
*/
/*
Q9. count-code
Write a Java method that takes a string parameter and returns the number of times that the string "code" appears anywhere in the given string, except we'll accept any letter for the 'd', so "cope" and "cooe" count.
For example:
countCode("aaacodebbb") → 1
countCode("codexxcode") → 2
countCode("cozexxcope") → 2
Q10. count-the-vowels
Write a function that accepts a string as a parameter and counts the number of vowels within the string (vowels include a, e, i, o , u - don't count 'y'). The function should return the number of vowels in the string.
Example output:
vowelCount('test string'); // returns 2
vowelCount('longer string with more vowels'); // returns 8
Q11. cut-a-string-at-character
Write a function to return a part of string after a specified character. The function should take two arguments, the first being the string, and the second being the character. The function should return only the part of the string that comes AFTER the specified character. In other words, the function should chop the string into two parts and return only the part that comes after the specified character.
Examples:
subStrAfterChars('this is a test string', 'a') // returns " test string"
subStrAfterChars('this is another test', 'o') // returns "ther test"
Q12. twelve-days
Print "The Twelve Days of Christmas", an English carol with a lot of repetition!
You can find the structure of the lyrics here: (The Twelve Days of Christmas Lyrics)[http://en.wikipedia.org/wiki/The_Twelve_Days_of_Christmas_%28song%29#Lyrics].
Use variables to store bits of repeated data and print the full lyrics.
Q13. scanner-ice-cream-start-up
Your friends just came up with a great new start-up idea: it's like Uber for ice cream! They've asked you to write a prototype Java program that will accept ice cream orders for delivery. The program should use a Scanner object to read user input and should:
Greet the user and ask them for a name.
Ask the user what flavor of ice cream they would like.
One-by-one, offer three toppings of your choice which the user will select by inputting 'yes'.
Calculate the total price of the order: the base ice cream costs $2.33 and each topping is an additional $0.33 cents.
Calculate a delivery wait time estimate, which should be a random number of minutes between 1 and 60.
Print an order confirmation to the console that contains the following details: the user's name, ice cream flavor, number of toppings, total price, and delivery wait time.
Here is an example interaction:
Welcome to our ice cream service! What's your name?
> Jim
Okay Jim, which flavor of ice cream would you like?
> vanilla
Would you like chocolate chips?
> yes
Would you like bananas?
> No
Would you like gummy bears?
> YES
Okay! A vanilla ice cream with 2 toppings. Your total is $2.99 and your ice cream will arrive in 12 minutes.
Homework submission
You can use repl.it to complete and submit the exercises.
If you haven't already, create an account on repl.it using your github account.
You may complete all of the exercises over the course of the week in a single session named "HW Week 1 - (Your name)". Remember to save your work as you go!
For each exercise, place a comment above the exercise with the question number and name, for example: // Q1. letter-to-yourself. Please make sure your exercises are in the correct order from top to bottom before submitting.
When you have completed all of the exercises, click the "Share" button then select Github to share the exercises to a Gist.
Copy the Gist URL and submit it using this form by Saturday 8/27 at 8pm.
*/
}
// Q1
public static void add(int add1, int add2) {
int addResult = add1 + add2;
System.out.println(addResult);
}
// Q2
public static void calculateAge(int birthYr, int currentYr) {
int calculateYr = currentYr - birthYr;
int calculatePreviousYr = calculateYr - 1;
System.out.println("You are either " + calculateYr + " or " + calculatePreviousYr);
}
// Q4
public static void endsly() {
String findLY = "endlessly";
if (findLY.charAt(findLY.length() - 2) == 'l' && findLY.charAt(findLY.length() - 1) == 'y') {
System.out.println("True");
} else {
System.out.println("False");
}
}
// Q7
public static void elide(String elideArg) {
String elideStr = elideArg;
if (elideStr.length() < 7) {
System.out.println(elideStr);
} else {
System.out.println(elideStr.substring(0,3) + "..." + elideStr.substring(elideStr.length() - 1));
}
}
}
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
>>> 22
You are either 29 or 28
They have the same count of 'x's and 'o's
True
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
Enter a food for the Hippos to eat: help
Yum!
Tha....
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
@edisplay
Copy link
Author

HW in progress/review...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment