Skip to content

Instantly share code, notes, and snippets.

@ellenia
Created September 25, 2017 02:55
Show Gist options
  • Save ellenia/2448f04b2b5c94072985f5ffc054e439 to your computer and use it in GitHub Desktop.
Save ellenia/2448f04b2b5c94072985f5ffc054e439 to your computer and use it in GitHub Desktop.
Java Projects
//Write a program that reads three whole numbers and displays the average of the three numbers.
import java.util.Scanner;
public class ProjectOne {
public static void main(String[] args){
Scanner keyScan = new Scanner(System.in);
System.out.println("This program will take three whole numbers, "
+ "and display the average of the three.");
System.out.println("\nEnter the first whole number");
int numberOne = keyScan.nextInt();
System.out.println("\nEnter the second number");
int numberTwo = keyScan.nextInt();
System.out.println("\nEnter the third number");
int numberThree = keyScan.nextInt();
int average = (numberOne + numberTwo + numberThree);
System.out.println("\nNumber one: " + numberOne
+ "\nNumber two: " + numberTwo
+ "\nNumber three: " + numberThree);
System.out.println("Number Average: " + average);
}
}
//Write a program that uses Scanner to read two Strings from the keyboard.
//Display each String, along with it's length, on two separate lines. Then create the new string and its length on a third line.
import java.util.Scanner;
public class ProjectTwo {
public static void main(String[] args){
Scanner keyScan = new Scanner(System.in);
System.out.println("This program will ask for two Strings,"
+ "then display them with their length."
+ "The program will then add the two strings,"
+ "separate them by a space and then give the new length.");
System.out.println("\nEnter the first String [Word]");
String wordOne = keyScan.next();
System.out.println("\nEnter the second String [Word]");
String wordTwo = keyScan.next();
int oneLength = wordOne.length();
int twoLength = wordTwo.length();
System.out.println("\nFirst String: " + wordOne + ". Length: " + oneLength);
System.out.println("Second String: " + wordTwo + ". Length: " + twoLength);
String wordThree = wordOne+ " " + wordTwo;
int threeLength = wordThree.length();
System.out.println("Thrid String: " + wordThree+ ". Length: " + threeLength);
}
}
//Write a program that reads the amount of a monthly mortgage payment and the amount still owed - the outstanding balance - and the amount still owed—the outstanding balance—and then displays the amount of the payment that goes to interest and the amount that goes to principal (i.e., the amount that goes to reducing the debt). Assume that the annual interest rate is 7.49 percent. Use a named constant for the interest rate. Note that payments are made monthly, so the interest is only one twelfth of the annual interest of 7.49 percent.
public class ProjectThree {
public static final double INTEREST_RATE = 7.49 / 12;
public static void main(String[] args){
double outstandingBalance = 1000000;
double monthlyPayment = outstandingBalance / 12;
double interestAmount = monthlyPayment * (INTEREST_RATE / 100);
double monthlyInterestTotal = interestAmount * 12;
double montlyPaymentInterestTotal = monthlyPayment + interestAmount;
double principalAmount = outstandingBalance + monthlyInterestTotal;
double roundMontlyPaymentInterestTotal = (double) Math.round(montlyPaymentInterestTotal * 100) / 100d;
double roundInterestAmount = (double) Math.round(interestAmount * 100) / 100d;
double roundMonthlyInterestTotal = (double) Math.round(monthlyInterestTotal * 100) / 100d;
double roundPrincipalAmount = (double) Math.round(principalAmount * 100) / 100d;
System.out.println("Your morgage amount left is: " + outstandingBalance);
System.out.println("This program will tell you how much you have left to pay in 12 months"
+ "with a annual interest of 7.49%");
System.out.println("\nThe monthly payment will be: R " + roundMontlyPaymentInterestTotal);
System.out.println("The monthly interest payment will be: " + roundInterestAmount);
System.out.println("Total interest paid: R " + roundMonthlyInterestTotal);
System.out.println("Your total back payment will be: R " + roundPrincipalAmount);
}
}
//Write a program that reads a four-digit integer, such as 1998, and then displays it, one digit per line, like so:
1
9
9
8
//Your prompt should tell the user to enter a four-digit integer. You can then assume that the user follows directions.
//(Hint: Use the division and remainder operators.)
import java.util.Scanner;
public class ProjectFour {
public static void main(String[] args){
Scanner keyScan = new Scanner(System.in);
System.out.println("This program will reads a four-digit integer,"
+ "and then displays it, one digit per line.");
System.out.println("\nEnter a four digit number.");
int numbers = keyScan.nextInt();
int firstNumber, secondNumber, thirdNumber, fourthNumber;
int newNumber = numbers;
firstNumber = newNumber / 1000;
newNumber = newNumber % 1000;
secondNumber = newNumber / 100;
newNumber = newNumber % 100;
thirdNumber = newNumber / 10;
newNumber = newNumber % 10;
fourthNumber = newNumber;
System.out.println("");
System.out.println(firstNumber);
System.out.println(secondNumber);
System.out.println(thirdNumber);
System.out.println(fourthNumber);
}
}
// Repeat the previous project, but read the four digit integer as a String. Use String methods instead of the hint.
import java.util.Scanner;
public class ProjectFive {
public static void main(String[] args){
Scanner keyScan = new Scanner(System.in);
System.out.println("This program will reads a four-digit String,"
+ "and then displays it, one digit per line.");
System.out.println("\nEnter a four digit number.");
String numbers = keyScan.nextLine();
String numberOne = numbers.substring(0, 1);
String numberTwo = numbers.substring(1, 2);
String numberThree = numbers.substring(2, 3);
String numberFour = numbers.substring(3);
System.out.println("");
System.out.println(numberOne);
System.out.println(numberTwo);
System.out.println(numberThree);
System.out.println(numberFour);
}
}
//Write a program the converts degrees from Fahrenheit to Celsius, using the formula:
//DegreesC = 5(DegreesF −32)/9. Prompt the user to enter a temperature in degrees Fahrenheit as a whole number without a fractional part. Then have the program display the equivalent Celsius temperature, including the fractional part to at least one decimal point. A possible dialogue with the user might be
//Enter a temperature in degrees Fahrenheit: 72 .72 degrees Fahrenheit is 22.2 degrees Celsius.
import java.util.Scanner;
public class ProjectSix {
public static void main(String[] args){
Scanner keyScan = new Scanner(System.in);
System.out.println("This program will convert degrees from "
+ "\nFahrenheit to Celsius.");
System.out.println("\nEnter a temperature in degrees Farenheid.");
double degreesF = keyScan.nextDouble();
double degreesC = 5 * (degreesF - 32) / 9;
double roundDegreesC = (double) Math.round(degreesC * 10) / 10d;
System.out.println("");
System.out.println(degreesF + " degrees Fahrenheit is " + roundDegreesC + " degrees Celsius");
}
}
//Write a program that reads a line of text and then displays the line, but with the first occurrence of hate changed to love. For example, a possible sample dialogue might be
Enter a line of text.
I hate you.
I have rephrased that line to read:
I love you.
You can assume that the word hate occurs in the input. If the word hate occurs more than once in the line, your program will replace only its first occurrence.
import java.util.Scanner;
public class ProjectSeven {
public static void main(String[] args){
Scanner keyScan = new Scanner(System.in);
System.out.println("This program will change the first occurrence of"
+ "\n the word 'hate', and change it to 'love'.");
System.out.println("\nEnter a phrase that contains the word 'hate' in it.");
String sentence = keyScan.nextLine();
String replace = sentence.replaceFirst("hate", "love");
System.out.println("\nYour sentence is : " + replace);
}
}
//Write a program that will read a line of text as input and then display the line with the first word moved to the end of the line. For example, a possible sample interaction with the user might be
Enter a line of text. No punctuation please.
Java is the language
I have rephrased that line to read:
Is the language Java
Assume that there is no space before the first word and that the end of the first word is indicated by a blank, not by a comma or other punctuation.
Note that the new first word must begin with a capital letter.
import java.util.Scanner;
public class ProjectEight {
public static void main(String[] args){
Scanner keyScan = new Scanner(System.in);
System.out.println("This program will change the first word in a sentence,"
+ "\n and move it to the end of the sentence.");
System.out.println("Enter any sentence.");
String sentence = keyScan.nextLine();
int space = sentence.indexOf(" ");
String firstWord = sentence.substring(0, space + 1);
String removeWord = sentence.replaceFirst(firstWord, "");
System.out.println("");
System.out.println(removeWord + " " + firstWord);
}
}
//Write a program that asks the user to enter a favorite color, a favorite food, a favorite animal, and the first name of a friend or relative. The program should then print the following two lines, with the user's input replacing the items in italics:
I had a dream that Name ate a Color Animal and said it tasted like Food!
For example, if the user entered blue for the color, hamburger for the food, dog for the animal, and Jake for the person's name, the output would be
I had a dream that Jake ate a blue dog and said it tasted like hamburger!
Don't forget to put the exclamation mark at the end.
import java.util.Scanner;
public class ProjectNine {
public static void main(String[] args){
Scanner keyScan = new Scanner(System.in);
System.out.println("This program will ask the you a series "
+ "\nof questions, based on the answers, the "
+ "\nprogram will the add them to a existing "
+ "\nsentence.");
System.out.println("\nEnter your favorite colour [lowercase]");
String wordOne = keyScan.next();
System.out.println("\nEnter your favorite food [lowercase]");
String wordTwo = keyScan.next();
System.out.println("\nEnter your favorite Animal [lowercase]");
String wordThree = keyScan.next();
System.out.println("\nEnter the first name of a friend or relative [uppercase]");
String wordfour = keyScan.next();
System.out.println("\nI had a dream that " + wordfour + " ate a " + wordOne + " " + wordThree + " "
+ "\nand said it tastes like " + wordTwo + "!");
}
}
//Write a program that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and a dollar, in 5-cent increments (25, 30, 35, . . . , 90, 95, or 100), and the machine accepts only a single dollar bill to pay for the item. For example, a possible dialogue with the user might be
Enter price of item
(from 25 cents to a dollar, in 5-cent increments): 45
You bought an item for 45 cents and gave me a dollar, so your change is
2 quarters,
0 dimes, and
1 nickel.
import java.util.Scanner;
public class ProjectTen {
public static void main(String[] args){
Scanner keyScan = new Scanner(System.in);
int bill = 100;
int change, halfDollar, quarter, dime, nickel;
System.out.println("This program takes a one dollar bill and"
+ "\ndeduct the price of an item, with that in"
+ "\nmind, it will then give exact change back.");
System.out.println("\nEnter the price of the item baught.");
System.out.println("The amount should no less than 5 and no more than 100");
System.out.println("The price should work in 5-cent increment [5, 10 , etc]");
int price = keyScan.nextInt();
change = bill - price;
halfDollar = change / 50;
change = change % 50;
quarter = change / 25;
change = change % 25;
dime = change / 10;
change = change % 10;
nickel = change / 5;
change = change % 5;
System.out.println("\nChange: ");
System.out.println("Half-Dollar: " + halfDollar);
System.out.println("Quarter(s): " + quarter);
System.out.println("Dime(s): " + dime);
System.out.println("Nickle(s): " + nickel);
}
}
//Write a program that reads a 4-bit binary number from the keyboard as a string and then converts it into decimal. For example, if the input is 1100, the output should be 12.
(Hint: Break the string into substrings and then convert each substring to a value for a single bit. If the bits are b0, b1, b2, and b3, the decimal equivalent is 8b0+ 4b1+ 2b2+ b3.)
import java.util.Scanner;
public class ProjectEleven {
public static void main(String[] args){
Scanner keyScan = new Scanner(System.in);
System.out.println("This program will read a users 4-bit binary number"
+ "\nfrom the keyboard as a String and then convert it"
+ "\ninto decimal.");
System.out.println("Enter your 4-bit binary number");
String binary = keyScan.nextLine();
String bitOne = binary.substring(0, 1);
String bitTwo = binary.substring(1, 2);
String bitThree = binary.substring(2, 3);
String bitFour = binary.substring(3, 4);
int dBitOne = Integer.parseInt(bitOne);
int dBitTwo = Integer.parseInt(bitTwo);
int dBitThree = Integer.parseInt(bitThree);
int dBitFour = Integer.parseInt(bitFour);
System.out.println((dBitOne 8) + (dBitTwo 4) + (dBitThree 2) + (dBitFour 1));
}
}
//Many private water wells produce only 1 or 2 gallons of water per minute. One way to avoid running out of water with these low-yield wells is to use a holding tank. A family of 4 will use about 250 gallons of water per day. However, there is a "natural" water holding tank in the casing (i.e., the hole) of the well itself. The deeper the well, the more water that will be stored that can be pumped out for household use. But how much water will be available?
Write a program that allows the user to input the radius of the well casing in inches (a typical well will have a 3 inch radius) and the depth of the well in feet (assume water will fill this entire depth, although in practice that will not be true since the static water level will generally be 50 feet or more below the ground surface). The program should output the number of gallons stored in the well casing. For your reference:
The volume of a cylinder is r2h , where r is the radius and h is the height. 1 cubic foot = 7.48 gallons of water.
For example, a 300-foot well full of water with a radius of 3 inches for the casing holds about 441 gallons of water—plenty for a family of 4 and no need to install a separate holding tank.
import java.util.Scanner;
public class ProjectTwelve {
public static final double PI = 3.142;
public static void main(String[] args){
Scanner keyScan = new Scanner(System.in);
System.out.println("This program ask the user to supply a radius[inches],"
+ "\nand depth[feet] to do the sum of how much water a well"
+ "\nwill have.");
System.out .println("\nEnter the depth of the well [in feet]");
double depth = keyScan.nextDouble();
System.out.println("\nEnter the radius of the well [inches]");
double radius = keyScan.nextDouble();
double inchToFeet = radius / 12;
double volume = PI (inchToFeet inchToFeet) * depth;
double galloons = volume * 7.48;
System.out.println("\nThe well can hold up to " + galloons + " gallons of water");
}
}
//The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you do no exercise. This is called your basal metabolic rate, or BMR.
The calories needed for a woman to maintain her weight is:
.
BMR = 655 + (4.3 x weight in pounds) + (4.7 x height in inches) - (4.7 x age in years)
The calories needed for a man to maintain his weight is:
.
BMR = 655 + (6.3 x weight in pounds) + (12.9 x height in inches) - (6.8 x age in years)
A typical chocolate bar will contain around 230 calories. Write a program that allows the user to input their weight in pounds, height in inches, and age in years. The program should then output the number of chocolate bars that should be consumed to maintain one's weight for both a woman and a man of the input weight, height, and age.
import java.util.Scanner;
public class ProjectThirteen {
public static void main(String[] args){
Scanner keyScan = new Scanner(System.in);
System.out.println("Using the BMR sum, this program will tell"
+ "\nthe user the number of chocolates he/she can"
+ "\neat to maintain one's weight for both male"
+ "\nand female.");
System.out.println("Note: Enter numbers without a decimal");
System.out.println("\nEnter your weight [Pounds]");
int weight = keyScan.nextInt();
System.out.println("\nEnter your hight [inches]");
int height = keyScan.nextInt();
System.out.println("\nEnter your age [years]");
int age = keyScan.nextInt();
double femaleBMR = 655 + (4.3 weight) + (4.7 height) - (4.7 * age);
int fChoc = (int) (femaleBMR / 230);
double maleBMR = 66 + (6.3 weight) + (12.9 height) - (6.8 * age);
int mChoc = (int) (maleBMR / 230);
System.out.println("\nTo maintain your weight without exercise you will need"
+ "\nto eat this many chocolate bars:");
System.out.println("\nIf your male: " + mChoc);
System.out.println("\nIf your female: " + fChoc);
}
}
//Repeat any of the previous programming projects using JOptionPane, which is described in the graphics supplement.
import javax.swing.JOptionPane;
public class ProjectFourteen extends JOptionPane {
public static void main(String[] args){
int bill = 100;
int change, halfDollar, quarter, dime, nickel;
String inputPrice = JOptionPane.showInputDialog("Enter price of item"
+ "\nThe amount should no less than 5 and no more than 100"
+ "\nThe price should work in 5-cent increment [5, 10 , etc]");
int price = Integer.parseInt(inputPrice);
change = bill - price;
halfDollar = change / 50;
change = change % 50;
quarter = change / 25;
change = change % 25;
dime = change / 10;
change = change % 10;
nickel = change / 5;
change = change % 5;
JOptionPane.showMessageDialog(null, "Half-Dollar: " + halfDollar
+ "\nQuarter(s): " + quarter
+ "\nDime(s): " + dime
+ "\nDime(s): " + nickel);
System.exit(0);
}
}
//Write a program that reads a String for a date in the format month / day / year and displays it in the format day . month . year, which is a typical format used in Europe.
For example, if the input is 06 /17/11, the output should be 17.06.11.
Your program should use JOptionPanefor input and output.
import javax.swing.JOptionPane;
public class ProjectFifteen extends JOptionPane {
public static void main(String[] args){
String americanFormat = JOptionPane.showInputDialog("This program will convert a American date format"
+ "\nto a European date format"
+ "\nEnter date as month/day/year");
String month = americanFormat.substring(0, 2);
String day = americanFormat.substring(3, 5);
String year = americanFormat.substring(6, 10);
String europeFormat = day + "." + month + "." + year;
String otherFormat = year + "." + month + "." + day;
JOptionPane.showMessageDialog(null, "American format: " + americanFormat
+ "\nEuropean format: " + europeFormat
+ "\nOther format: " + otherFormat);
System.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment