Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Exercises;
- import java.util.Scanner;
- public class CountVowels {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner s = new Scanner(System.in);
- String text;
- System.out.println("Enter text: ");
- text = s.nextLine();
- char[] textArray = text.toCharArray();
- int vowelCount = 0;
- for(char c : textArray) {
- String newChar = String.valueOf(c);
- if(newChar.equalsIgnoreCase("o") || newChar.equalsIgnoreCase("i")
- || newChar.equalsIgnoreCase("o") || newChar.equalsIgnoreCase("u")) {
- vowelCount++;
- }
- }
- System.out.println("The number of vowels in \"" + text + "\" is " + vowelCount);
- s.close();
- }
- }
- package Exercises;
- import java.util.Random;
- public class DiceRolls {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Random r = new Random();
- System.out.format("%-5s %4s %6s", "Dice 1", "Dice 2", "Total");
- for(int i = 1; i < 5; i++) {
- int d1 = r.nextInt(6) +1;
- int d2 = r.nextInt(6) +1;
- System.out.format("%3s %6s %8s", "\n" + d1, d2, (d1 + d2));
- }
- }
- }
- package Exercises;
- import java.util.Scanner;
- public class DigitsDisplay {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner s = new Scanner(System.in);
- int number;
- System.out.println("Enter a positive integer");
- number = s.nextInt();
- if(number >= 0) {
- for(int i = 0; i < String.valueOf(number).length(); i++) {
- System.out.println(String.valueOf(number).charAt(i));
- }
- } else {
- System.err.println("The integer must be positive.");
- }
- s.close();
- }
- }
- package Exercises;
- import java.util.Scanner;
- public class DigitsSum {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner s = new Scanner(System.in);
- int number;
- int sum = 0;
- System.out.println("Enter a positive integer: ");
- number = s.nextInt();
- if(number >+ 0) {
- for(int i = 0; i < String.valueOf(number).length(); i++) {
- char n = String.valueOf(number).charAt(i);
- sum += Integer.valueOf(String.valueOf(n));
- }
- System.out.println("The sum of the digits is: " + sum);
- } else {
- System.err.println("The integer must be positive.");
- }
- s.close();
- }
- }
- package Exercises;
- import java.util.Scanner;
- public class ElapsedTimeCalculator {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner s = new Scanner(System.in);
- int startingHour, elapsedHours;
- String timeOfDay;
- System.out.println("Enter the starting hour: ");
- startingHour = s.nextInt();
- System.out.println("Enter AM or PM: ");
- timeOfDay = s.next().toUpperCase();
- System.out.println("Enter the number of elapsed hours: ");
- elapsedHours = s.nextInt();
- int newTime = convertTo24Hour(startingHour, timeOfDay);
- newTime =+ elapsedHours;
- if(newTime < 12) {
- System.out.println("the time is: " + newTime + ":00 AM");
- } else {
- int d = newTime / 12;
- int d1 = newTime % 12;
- if(Math.floor(Double.valueOf(d) / 2.0) == (Double.valueOf(d)) / 2.0) {
- System.out.println("The time is: " + d1 + "00 PM");
- } else {
- System.out.println("The time is: " + d1 + "00 AM");
- }
- }
- s.close();
- }
- public static int convertTo24Hour(int time, String timeOfDay) {
- if(timeOfDay == "PM") {
- return 12 + time;
- } else {
- return time;
- }
- }
- }
- package Exercises;
- import java.util.Random;
- import java.util.Scanner;
- public class GuessingGame {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner s = new Scanner(System.in);
- Random r = new Random();
- int n1 , n2;
- n1 = r.nextInt(20) + 1;
- System.out.println("Enter a number between 1 and 20:");
- n2 = s.nextInt();
- if(n2 < 1 || n2 > 20) {
- System.err.println("Number must be between 1 and 20.");
- } else {
- System.out.println("Computer's Number:" + n1);
- if(n1 == n2) {
- System.out.println("You got the number!");
- } else {
- System.out.println("Better luck next time.");
- }
- }
- s.close();
- }
- }
- package Exercises;
- import java.util.Scanner;
- public class Monogram {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner s = new Scanner(System.in);
- String first, middle, last;
- System.out.println("Enter your first name:");
- first = s.next();
- System.out.println("Enter your middle initial:");
- middle = s.next();
- System.out.println("Enter your last name:");
- last = s.next();
- System.out.println("\nYour monogram is: " + String.valueOf(first.charAt(0)) .toLowerCase() +
- middle.toUpperCase() +
- String.valueOf(last.charAt(0)).toLowerCase());
- s.close();
- }
- }
- package Exercises;
- import java.util.Scanner;
- public class RemoveString {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner s = new Scanner(System.in);
- String text, replace;
- System.out.println("Enter a sentence:");
- text = s.nextLine();
- System.out.println("Enter a string:");
- replace = s.next();
- System.out.println(text.replaceAll(" " + replace, ""));
- s.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement