Skip to content

Instantly share code, notes, and snippets.

@gitaficionado
gitaficionado / SortThreeNumbers_06_04.java
Created November 29, 2019 19:47
(Display an integer reversed) Write a method with the following header to display an integer in reverse order:public static void reverse(int number)
/**
*(Display an integer reversed) Write a method with the following header
*to display an integer in reverse order:public static void reverse(int number)
*For example, reverse(3456) displays 6543. Write a test program that prompts the
*user to enter an integer and displays its reversal.
*/
public class SortThreeNumbers_06_04 {
@gitaficionado
gitaficionado / PalindromeInteger_06_03.java
Created November 29, 2019 18:35
Use the reverse method to implement isPalindrome. A number is a palin-drome if its reversal is the same as itself. Write a test program that prompts the user to enter an integer and reports whether the integer is a palindrome. See page 215 in the Liang text more informaiton.
import java.util.Scanner;
/**
*Write the methods with the following headers
*Return the reversal of an integer, i.e., reverse(456) returns 654
*public static int reverse(int number)
*Return true if number is a
*palindromepublic static boolean isPalindrome(int number)
@gitaficionado
gitaficionado / SumDigitsInteger_06_02.java
Created November 29, 2019 18:05
Write a method that computes the sum of the digits in an integer. Use the following method header:public static int sumDigits(long n) See page P. 215 in the Liang textbook for more informaiton.
public class SumDigitsInteger_06_02 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter a number: ");
long value = input.nextLong();
System.out.println("The sum of digits for " + value +
" is " + sumDigits(value));
}
@gitaficionado
gitaficionado / CountUppercaseLetter_Exercise05_50.java
Created November 6, 2019 13:53
(Count uppercase letters) Write a program that prompts the user to enter a string and displays the number of the uppercase letters in the string.
public class CountUppercaseLetter_Exercise05_50 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter a string: ");
// Create a string with user input
_______________________________________
//Create a variable of int type called total
____________________
@gitaficionado
gitaficionado / CountVowelsAndConsonantsExercise_05_49.java
Created November 6, 2019 13:49
(Count vowels and consonants) Assume letters A,E,I,O, and U as the vowels. Write a program that prompts the user to enter a string and displays the number of vowels and consonants in the string
import java.util.Scanner;
public class CountVowelsAndConsonantsExercise_05_49 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
//Create string input form user
_____________________________
int countVowels = __;
int countConsonants = __;
@gitaficionado
gitaficionado / ProcessStringExercise_05_48.java
Created November 6, 2019 13:45
(Process string) Write a program that prompts the user to enter a string and dis-plays the characters at odd positions
import java.util.Scanner;
public class ProcessStringExercise_05_48 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
_________________________________________
Create a for loop that will look for each odd letter in the string.
@gitaficionado
gitaficionado / ReverseAStringExercise_05_46.java
Created November 6, 2019 05:50
(Reverse a string) Write a program that prompts the user to enter a string and displays the string in reverse order.
import java.util.Scanner;
public class ReverseAStringExercise_05_46 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
__________________________________
String result = "";
for (int i = __; ___ < s.length(); ____) {
@gitaficionado
gitaficionado / FindSalesAmountExercise_05_39.java
Created November 6, 2019 05:49
(Financial application: find the sales amount) You have just started a sales job in a department store. Your pay consists of a base salary and a commission. The base salary is $5,000. See page 183 for additional information.
public class FindSalesAmountExercise_05_39 {
/** Main method */
public static void main(String[] args) {
// The commission sought
final double COMMISSION_SOUGHT = 25000;
final double INITIAL_SALES_AMOUNT = 0.01;
double commission = 0;
double salesAmount = INITIAL_SALES_AMOUNT;
do {
@gitaficionado
gitaficionado / LotteryWithTwoDigits_5_32.java
Created November 6, 2019 05:40
(Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a two-digit number. The two digits in the number are distinct. (Hint: Generate the first digit. Use a loop to continuously generate the second digit until it is different from the first digit.
public class LotteryWithTwoDigits_5_32 {
public static void main(String[] args) {
// Generate the first digit
__________________ = ____________________
// Generate the second digit
int second = (int)(Math.random() * 10);;
while (first == _________) {
second = (int)(Math.random() * 10);
@gitaficionado
gitaficionado / DisplayFirstDayOfMonth_05_28.java
Created November 6, 2019 05:35
(Display the first days of each month) Write a program that prompts the user to enter the year and first day of the year, and displays the first day of each month in the year. For example, if the user entered the year 2013, and 2 for Tuesday, January 1, 2013, your program should display the following output:January 1, 2013 is Tuesday...December …
public class DisplayFirstDayOfMonth_05_28 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
// Prompt the user to enter input
System.out.print("Enter a year: ");
int year = input.nextInt();
System.out.print("Enter the first day of the year: ");
int firstDay = input.nextInt();