Skip to content

Instantly share code, notes, and snippets.

@gitaficionado
gitaficionado / HangmanDemo.java
Created May 21, 2019 01:13
Write a program to play a word-guessing game like Hangman. It must randomly choose a word from a list of words. It must stop when all the letters are guessed. It must give them limited tries and stop after they run out. It must display letters they have already guessed (either only the incorrect guesses or all guesses).
import java.util.Scanner;
public class Hangman
{
public static void main( String[] args )
{
String[] word_list = { "ruby", "python", "java", "tootsie", "chairlift", "kittens" };
String rand_word;
char[] hidden_word;
@gitaficionado
gitaficionado / gist:49cb8e2b57493ca448d6e062df43f18e
Created February 2, 2018 23:11
Write a program that reads the integers between 1 and 100 and counts the occurrences of each
/*
Count occurrence of numbers) Write a program that reads the integers between 1
and 100 and counts the occurrences of each. Assume the input ends with 0. Here
is a sample run of the program:
Programming Exercises 277
Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
@gitaficionado
gitaficionado / ColorGuess.py
Created March 11, 2021 16:32
Prompt the user to guess your favorite color. Using a while loop, if the user didn't guess your favorite color [pick one for this activity] then tell them they are incorrect, then ask them again. Whenever they guess the color correctly, output that they are correct and how many guesses it took them.
'''
2. Prompt the user to guess your favorite color. Using a while loop, if the user didn't guess your favorite color [pick one for this activity] then tell them they are incorrect, then ask them again. Whenever they guess the color correctly, output that they are correct and how many guesses it took them.
Create a variable and assign it the value of 1
Prompt the user to guess your favorite color
While their guess is not equal to your favorite color
Tell them they are incorrect
Prompt them to guess again
Add one to the variable above
Output to the user, they were correct and how many attempts it took using the variable
@gitaficionado
gitaficionado / gist:d764e92434621bee5b7e525c0f195a5b
Created February 2, 2018 23:24
Write a program that generates 100 random integers between 0 and 9 and displays the count for each number.
/*
(Count single digits) Write a program that generates 100 random integers between
0 and 9 and displays the count for each number. (Hint: Use an array of ten integers,
say counts, to store the counts for the number of 0s, 1s, . . . , 9s.)
*/
public class CountSingleDigits {
public static void main(String[] args) {
int[] ________ = new int[___];
@gitaficionado
gitaficionado / SumNumbers.py
Created March 11, 2021 16:29
Ask the user how many numbers for which they want to calculate the sum. Using a for loop, prompt the user to enter that many numbers, one-by-one, keeping track of the sum. At the end, after the user entered all numbers, output the sum.
'''
3. Ask the user how many numbers for which they want to calculate the sum. Using a for loop, prompt the user to enter that many numbers, one-by-one, keeping track of the sum. At the end, after the user entered all numbers, output the sum.
Create a variable and assign it the value of 0
Prompt the user for how many numbers they have
With a for loop, set it to repeat enough times to get all their values
Prompt the user for a number
Add that number to the variable that started as 0
Output to the user, the sum of all values
@gitaficionado
gitaficionado / FindFutureDates_03_05.java
Created September 28, 2019 13:44
Write a program that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1, ..., and Saturday is 6). Also prompt the user to enter the number of days after today for a future day and dis-play the future day of the wk. See page pg. 100 for more details.
import java.util.Scanner;
public class FindFutureDates_03_05 {
public static void main(String[] args) {
// Prompt the user to enter an integer for today
System.out.print("Enter today’s day: ");
_________________________________________
@gitaficionado
gitaficionado / Lottery_03_15
Created October 11, 2019 13:21
(Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a three-digit number. See page pg. 102 for more infromation .
public class Lottery_03_15 {
public static void main(String[] args) {
// Generate a lottery using Math.random here for 3 digits using a variable called lottery.
// Prompt the user to enter a guess by asking them to enter three digits. Variable must be called guess and be intialized as an it.
java.util.Scanner input = new java.util.Scanner(System.in);
// Get digits
int l1 = lottery / 100;
@gitaficionado
gitaficionado / DisplayLeapYear_Exercise05_27.java
Created November 6, 2019 05:31
(Display leap years) Write a program that displays all the leap years, ten per line, from 101 to 2100, separated by exactly one space. Also display the number of leap years in this period
public class DisplayLeapYear_Exercise05_27 {
public static void main(String[] args) {
int count = __;
for (int year = ___; year <= ______; year++) {
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
System.out.print((++count % 10 == 0)? year + "\n": year + " ");
}
System.out.println("The number of leap year is " + count);
}
@gitaficionado
gitaficionado / CheckSocialSecurityNumber_04_21.java
Created September 30, 2019 03:22
(Check SSN) Write a program that prompts the user to enter a Social Security number in the format DDD-DD-DDDD, where D is a digit. Your program should check whether the input is valid. See page 144 for a sample run.
import java.util.Scanner;
public class CheckSocialSecurityNumber_04_21 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a SSN: ");
// create social security number variable
@gitaficionado
gitaficionado / ProcesingAList.py
Created March 11, 2021 16:43
Create a new program in Repl.it called "Lists." Your goal for this program is to create sections of code that will perform different outputs with the list called, names which contains the following values - Peter, Bruce, Steve, Tony, Natasha, Clint, Wanda, Hope, Danny, Carol and the list called numbers which contains the following values - 100, …
'''
Create a new program in Repl.it called "Lists." Your goal for this program is to create sections of code that will perform different
outputs with the list called, names which contains the following values - Peter, Bruce, Steve, Tony, Natasha, Clint, Wanda, Hope,
Danny, Carol and the list called numbers which contains the following values - 100, 50, 10, 1, 2, 7, 11, 17, 53, -8, -4, -9, -72, -64,
-80. At the beginning of your program, add in any comments and create the two lists. Then create the following:
A loop that will output every other name in the names list.
A loop that will output only the positive numbers in the numbers list.
A loop that will output the sum of all the values in the numbers list.
A loop that will output only the numbers that are odd.