gaddis-ch4

CoinsToss 4-25

posted Mar 5, 2014, 10:05 AM by dldar masud   [ updated Mar 5, 2014, 10:05 AM by Karwan Jacksi ]


package cointoss;

import java.util.Random;
/*
 This program simulates 10 tosses of a coin.
 */
public class CoinToss {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // Create a Random object to generate random numbers.
        Random rand = new Random();
        
         // Simulate the coin tosses.
         for (int count = 0; count < 10; count++)
         {
           if (rand.nextInt(2)== 0)
           System.out.println("Tails");
           else
           System.out.println("Heads");
         }
       
    }
}

RollDice 4-24

posted Mar 5, 2014, 9:57 AM by dldar masud   [ updated Mar 5, 2014, 9:57 AM by Karwan Jacksi ]


package rolldice;

import java.util.Scanner;
import java.util.Random;

/*
 This program simulates the rolling of dice.
 */
public class RollDice {

   
    public static void main(String[] args) 
    {
        String again ="y";  // To control the loop
        int die1;           // To hold the value of die U
        int die2;           // to hold the value of die »2
        // Create a Scanner object to read keyboard input.
        Scanner keyboard = new Scanner(System.in);
        
        // Create a Random object to generate randon numbers.
        Random rand = new Random();
        
        // Simulate rolling the dice.
        while (again.equalsIgnoreCase( "y"))
        {
         System.out.println("Rolling the dice ... " ) ;
         die1 = rand.nextInt(6) + 1;
         die2 = rand.nextInt(6) + 1;
         System.out.println("Their values are : " );
         System.out.println(die1 + "   " + die2 );
         System.out.print("Roll them again (y - yes)? ");
         again = keyboard.nextLine();
        }
    }
}

Mathtutor 4-23

posted Mar 5, 2014, 9:37 AM by dldar masud   [ updated Mar 5, 2014, 9:44 AM by Karwan Jacksi ]


package mathtutor;

import java.util.Scanner; // Needed for the Scanner class
import java.util.Random; // Needed tor the Random class

/*
This program demonstrates the Random class.
 */

public class MathTutor {

   
    public static void main(String[] args) 
    {
        int number1;    // A number
        int number2;    // Another number
        int sum;        // The sum of the numbers
        int userAnswer; // The user's answer


       // Create a Scanner object tor keyboard input.
       Scanner keyboard = new Scanner(System.in);
     
       // Create a Random class object.
       Random randomNumbers = new Random();
     
       // Get two random numbers.
       number1 =randomNumbers.nextInt(100);
       number2 = randomNumbers.nextInt(100);
       
       // Display an addition problem.
       System.out.println("What is the answer to the"+
                          "following problem?");
       System.out.print(number2 + " + " +
       number2 + " =? " ) ;
       
       // Calculate the answer,
       sum = number1 + number2;
       
       // Get the user's answer.
       userAnswer = keyboard.nextInt();
       
       // Display the user's results,
       if (userAnswer == sum)
       System.out.println("Correct!");
       else
       {
          System.out.println("Sorry, wrong answer. "+
                             "The correct answer i s "+
                             sum);
       }
        
    }
}

FileWriteDemo 4-22

posted Mar 5, 2014, 9:02 AM by dldar masud   [ updated Mar 5, 2014, 9:02 AM by Karwan Jacksi ]



package filewritedemo;

import java.util.Scanner;  // Needed for Scanner class
import java.io.*;          // Needed for File and IOException


public class FileWriteDemo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws IOException {
        // TODO code application logic here
        String filename;      // Filename
        String friendName;    // Friend's name
        int numFriends;       // Number of friends
        
    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);
    
    // Get the number of friends.
    System.out.print("How many friends do you have? ");
    numFriends = keyboard.nextInt();

    // Consume the remaining newline character,
     keyboard.nextLine();
     
    // Get the filename.
    System.out.print("Enter the filename: " );
    filename = keyboard.nextLine();
    
    // Make sure the file does not exist.
    File file =new File(filename);
    if (file.exists())
    {
    System.out.println("The file=+"+filename
                     + " already exists." );
    System.exit(0);
    }
    //Open the file.
    PrintWriter outputFile=new PrintWriter(file);
    
    // Get data and write it to the file.
    for (int i = 1; i <= numFriends; i++)
    {
        // Get the name of a friend.
        
        System.out.print("Enter the nane of friend " +
                         "number " + i + ": ");
        friendName =  keyboard.nextLine();
        
        // Write the name to the file.
        outputFile.println(friendName);
    }
    
    // Close the t i l e.
    outputFile.close();
    System.out.println("Data written to the file. " );
    }
}

4-21 FileSum2

posted Mar 5, 2014, 2:30 AM by Dlovan ismail   [ updated Mar 5, 2014, 2:30 AM by Karwan Jacksi ]

//This version of the program confirms that the Numbers.txt file exists before opening it.

package filesum2;
import java.io.*;
import java.util.Scanner;

public class FileSum2
{
    public static void main(String[] args)throws IOException
    {
           double sum = 0.0; // Accumulator, initialized to 0
         
           // Make sure the file exists.
           File file = new File( "Numbers.txt");
           if (!file.exists())
           {
               System.out.println("The file Numbers.txt is not found.");
               System.exit(0);
           }
           
           // Open the file for reading.
           Scanner inputFile = new Scanner(file);
           
           // Read all of the values from the file
           // and calculate their total.
           while (inputFile.hasNext())
           {
               // Read a value from the file.
               double number = inputFile.nextDouble();

               // Add the number to sum.
               sum = sum + number;
           }

           // Close the file.
           inputFile.close();

           // Display the sum of the numbers.
           System.out.println("The sum of the numbers in Numbers.txt is " + sum);
           
    }  
}

4-20 FileSum

posted Mar 5, 2014, 2:16 AM by Dlovan ismail   [ updated Mar 5, 2014, 2:16 AM by Karwan Jacksi ]

//This program reads a series of numbers from a file and accumulates their sum.

package filesum;
import java.util.Scanner;
import java.io.*;

public class FileSum 
{
    public static void main(String[] args)throws IOException
    {
           double sum = 0.0; // Accumulator, initialized to 0

           // Open the file for reading.
           File file = new File( "Numbers.txt");
           Scanner inputFile = new Scanner(file);

           // Read all of the values from the file
           // and calculate their total.
           while (inputFile.hasNext())
           {
               // Read a value from the file.
               double number = inputFile.nextDouble();

               // Add the number to sum.
               sum = sum + number;
           }

           // Close the file.
           inputFile.close();

           // Display the sum of the numbers.
           System.out.println("The sum of the numbers in Numbers.txt is " + sum);
       
    }   
}

4-19 FileReadDemo

posted Mar 5, 2014, 1:45 AM by Dlovan ismail   [ updated Mar 5, 2014, 1:45 AM by Karwan Jacksi ]

//This program reads data from a file.

package filereaddemo;
import java.util.Scanner;  // Needed for the Scanner class
import java.io.*;                   // Needed for the File and lOException

public class FileReadDemo 
{
    public static void main(String[] args)throws IOException
    {
            // Create a Scanner object for keyboard input.
            Scanner keyboard = new Scanner(System.in);

            // Gat the filename.
            System.out.print("Enter the filename: ");
            String filename = keyboard.nextLine();

            // Open the file.
            File file = new File(filename);
            Scanner inputFile = new Scanner(file);

            // Read lines from the file until no more are left.
            while (inputFile.hasNext())
            {
                // Read the next name.
                String friendName = inputFile.nextLine();

                // Display the last name read.
                System.out.println(friendName);
            }

            // Close the file.
            inputFile.close();
        
    }   
}

4-18 ReadFirstLine

posted Mar 5, 2014, 1:28 AM by Dlovan ismail   [ updated Mar 5, 2014, 1:28 AM by Karwan Jacksi ]

//This program reads the first line from a file.

package readfirstline;
import java.util.Scanner; // Needed for Scanner class
import java.io.*;                  // Needed for File and IOException

public class ReadFirstLine 
{
    public static void main(String[] args)throws IOException
    {
            // Create a Scanner object for keyboard input.
            Scanner keyboard = new Scanner(System.in);

            // Get the file name.
            System.out.print("Enter the name of a file : " );
            String filename = keyboard.nextLine();

            // Open the file.
            File file = new File(filename);
            Scanner inputFile = new Scanner(file);

            // Read the first line from the file.
            String line = inputFile.nextLine();

            // Display the line.
            System.out.println("The first line in the file is : " );
            System.out.println(line);

            // Close the file.
            inputFile.close();
        
    }  
}

4-17 FileWriteDemo

posted Mar 5, 2014, 1:05 AM by Dlovan ismail   [ updated Mar 5, 2014, 1:05 AM by Karwan Jacksi ]

//This program writes data to a file.

package filewritedemo;
import java.util.Scanner;// Needed tor Scanner class
import java.io.*;                 // Needed for File I/O classes

public class FileWriteDemo 
{
    public static void main(String[] args)throws IOException 
    {
            String filename;         // File name
            String friendName;   // Friend's
            int numFriends;          // Number of friends

            // Create a Scanner object for keyboard input.
            Scanner keyboard = new Scanner(System.in);

            // Get the number of friends.
            System.out.print("How many friends do you have? " );
            numFriends = keyboard.nextInt();

            // Consume the remaining newline character.
            keyboard.nextLine();

            // Get the filename.
            System.out.print("Enter the filename: " );
            filename = keyboard.nextLine();

            // Open the file.
            PrintWriter outputFile = new PrintWriter(filename);

            // Get data and write it to the file.
            for (int i = 1;i<=numFriends; i++)
            {
            // Get the name of a friend.
            System.out.print("Enter the name of friend number " + i + ":");
            friendName = keyboard.nextLine();

            // Write the name to the file.
            outputFile.println(friendName);
            }

            // Close the file.
            outputFile.close();
            System.out.println("Data written to the file . " );
        
    }
}

StairStepattern 4-16

posted Mar 4, 2014, 12:21 PM by dldar masud   [ updated Mar 4, 2014, 12:21 PM by Karwan Jacksi ]

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stairstepattern;
import java.util.Scanner;
/*
   This program displays a stairstepattern.
 */
public class StairStepattern {

   
    public static void main(String[] args) {
        // TODO code application logic here

        final int NUM_STEPS =6;

     for (int r = 0; r < NUM_STEPS; r++)
      {
        for (int c = 0; c < r; c++)

          System.out.print(" ");

          System.out.println("#");
       }
    }
}

1-10 of 23