Skip to content

Instantly share code, notes, and snippets.

@JorgeOlvera
Created November 14, 2013 05:19
Show Gist options
  • Save JorgeOlvera/7461807 to your computer and use it in GitHub Desktop.
Save JorgeOlvera/7461807 to your computer and use it in GitHub Desktop.
Looping Lab
import java.io.*;
import java.util.Scanner;
public class CountingLetters {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
String firstWord, secondWord
System.out.println("Enter first word: ");
String firstWord = input.next();
System.out.println("Enter second word: ");
String secondWord = input.next();
String fin = firstWord + secondWord;
int len = fin.length();
int dotlen = 0;
if(len<=30) dotlen = 30-len;
String strdot="";
for(int i=1;i<=dotlen;i++) strdot+=".";
System.out.println("Output String : " +firstWord+strdot+secondWord);
}
}
/*Write a program (save it as CreditCard) that asks for the
monthly payment, then writes out the balance and total payments so far
for every succeeding month until the balance is exactly zero.
NOTE: Your program must be prepared to verify if the balance
falls below the amount of the monthly payment to write out
the final payment that will bring the balance to exactly zero. #*/
import java.io.*;
import java.util.Scanner;
public class CreditCard {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the monthly payment: ");
double payed = input.nextDouble();
int month = 0;
double owed = 1000;
System.out.println("Month:" + month + "Balance:" + owed + "Total payed: " + "0.000");
while (owed > payed)
{
owed = owed * 1.015;
owed = owed - payed;
System.out.println("Month: " + (month = month + 1) + " Balance: " + owed + " Total payed: " + (pay * month));
}
System.out.println("Month: " + (month = month + 1) + " Balance: 0.0" + " Total payed: " + (pay * month - owed));
}
}
/*calculates miles per gallon for a list of cars.
The data for each car consists of initial odometer reading,
final odometer reading, and number of gallons of gas.
The user signals that there are no more cars by entering
a negative initial odometer reading.*/
import java.util.Scanner;
public class MilesPerGallon{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
double initialReading, finalReading, gallons, diff, milesPerGallon;
System.out.println("Please introduce the car's initial odometer reading.");
initialReading = input.nextInt();
while (initialReading > 0){
System.out.println("Please introduce the cars final odometer reading.");
finalReading = input.nextInt();
System.out.println("Please introduce the gallons of gas.");
gallons = input.nextInt();
diff = finalReading - initialReading;
milesPerGallon = diff/gallons;
System.out.println("Miles Per Gallon: " + milesPerGallon);
System.out.println("Please introduce the car's initial odometer reading.");
initialReading = input.nextInt();
}
System.out.println("Thank you for using this service. \nGood-bye!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment