Home » » java program: letter counter and running the race

java program: letter counter and running the race

Written By Unknown on Tuesday, October 8, 2013 | 11:00 AM

Letter Counter
Write a program that asks the user to enter a string, and then asks the user to
enter a character. The program should count and display the number of times
that the specified character appears in the string.
import java.util.*;
/*
 * Course: ITECH 2120_Intro of programming JAVA
 * Instructor: Dr. Tacksoo Im
 * Project: Letter count_Assignment3
 * Author: Mohan Maharjan
 * Date: October 14 2013
 * Version: 1
 */
public class LetterCounter
{
     public static void main(String[] args)
     {
           Scanner scan = new Scanner(System.in);
           //users to write a letter
           System.out.println("Please write a short letter");
           String letter = scan.nextLine();
          
           // users to enter a word to count
           System.out.println("Please type a word to count");
           char countChar = scan.next().charAt(0);
    
           int count =0;
           for (int i = 0; i <letter.length(); i++)
           {
                if (letter.charAt(i)== countChar)
           {
                count++;
           }
         }
                System.out.println("The total matched word "+ countChar +
                " appears in the letter = " + count);
                scan.close();
     }
}

Running the Race
Write a program that asks for the names of three runners and the time, in
Minutes, it took each of them to finish a race. The program should display the
names of the runners in the order that they finis
import java.util.*;
/*
 * Course: ITECH 2120_Intro of programming JAVA
 * Instructor: Dr. Tacksoo Im
 * Project: Running the Race_Assignment3
 * Author: Mohan Maharjan
 * Date: October 14 2013
 * Version: 1
 */
public class RunningTheRace
{
     public static void main(String[] args)
     {
           String firstPlace,secondPlace,thirdPlace, firstName, secondName, thirdName;
           double firstRunner, secondRunner, thirdRunner;
           System.out.println("This application is created to display runners' position."+"\n" +"NO ties possible between them.");
           //get the first runner's info
           Scanner scan = new Scanner(System.in);
           System.out.println("#1 Please enter your name: ");
           firstName = scan.nextLine().toUpperCase();
           System.out.println("#1 Enter your finishing time: ");
           firstRunner = scan.nextDouble();
    
          
           //get the second runner's info
           Scanner scan2 = new Scanner (System.in);
           System.out.println("#2 Please enter your name: ");
           secondName = scan2.nextLine().toUpperCase();
           System.out.println("#2 Enter your finishing time: ");
           secondRunner = scan2.nextDouble();
          
          
           //get the third runner's info
           Scanner scan3 = new Scanner(System.in);
           System.out.println("#3 Please enter your name: ");
           thirdName = scan3.nextLine().toUpperCase();
           System.out.println("#3 Enter your finishing time: ");
           thirdRunner = scan3.nextDouble();
          
           //determine for the first position
           if(firstRunner < secondRunner && firstRunner < thirdRunner)
                {
                     firstPlace = firstName;
                }
           else if(secondRunner < firstRunner && secondRunner < thirdRunner)
                {
                     firstPlace = secondName;
                }
           else
                {
                     firstPlace = thirdName;
                }
                     //determine for the first position
                     if(firstRunner < secondRunner && firstRunner > thirdRunner)
                     {
                            secondPlace = firstName;
                     }
                     else if(firstRunner < thirdRunner && firstRunner > secondRunner)
                     {
                            secondPlace = firstName;
                     }
                     else if(secondRunner < firstRunner && secondRunner > thirdRunner)
                     {
                            secondPlace = secondName;
                     }
                     else if(secondRunner < thirdRunner && secondRunner > firstRunner)
                     {
                            secondPlace = secondName;
                     }
                     else
                     {
                           secondPlace = thirdName;
                     }
           //determine for the first position
           if(firstRunner > secondRunner && firstRunner > thirdRunner)
           {
                thirdPlace = firstName;
           }
           else if(secondRunner > firstRunner && secondRunner > thirdRunner)
           {
                thirdPlace = secondName;
           }
           else
           {
                thirdPlace = thirdName;
           }
                //printout the result
                System.out.println("Congratulations! "+ firstPlace + ", you are the first Position.");
                System.out.println("Congratulations! "+ secondPlace + ", you are the second position.");
                System.out.println("Congratulations! "+ thirdPlace + ", you are the third position.");
           scan.close();
           scan2.close();
           scan3.close();
     }
}



Share this article :

0 comments:

Post a Comment

 
Support : Creating | CollegeContents | Students work
Copyright © 2011. TestsInfo - All Rights Reserved
Created by Creativity Published by Leadership
Proudly powered by Vision