b

b

Tuesday, February 23, 2016

ChangingPeople.java & Person.java

 ChangingPeople.java & Person.java

//*************************************************************************************
//  ChangingPeople.java
//*************************************************************************************

import java.util.*;
public class ChangingPeople{
   
   public static void main (String[] args){
   
       Person person1 = new Person("Sally", 13);
       Person person2 = new Person("Sam", 15);
       int age = 21;
       String name = "Jill";
       System.out.println ("\nParameter Passing... Original values...");
       System.out.println ("person1: " + person1);
       System.out.println ("person2: " + person2);
       System.out.println ("age: " + age + "\tname: " + name + "\n");

       changePeople (person1, person2, age, name);
     
       System.out.println ("\nValues after calling changePeople...");
       System.out.println ("person1: " + person1);
       System.out.println ("person2: " + person2);
       System.out.println ("age: " + age + "\tname: " + name + "\n");
   }
   public static void changePeople (Person p1, Person p2, int age, String name){
       System.out.println ("\nInside changePeople... Original parameters...");
       System.out.println ("person1: " + p1);
       System.out.println ("person2: " + p2);
       System.out.println ("age: " + age + "\tname: " + name + "\n");
       Person p3 = new Person (name, age);
       p2 = p3;
       p2.changeName(name);
       p2.changeAge(age);
       name = "Jack";
       age = 101;
       p1.changeName (name);
       p1.changeAge (age);
       System.out.println ("\nInside changePeople... Changed values...");
       System.out.println ("person1: " + p1);
       System.out.println ("person2: " + p2);
       System.out.println ("age: " + age + "\tname: " + name + "\n");
   }
}

//*****************************************************
// Person.java
//*****************************************************
public class Person {

    public Person(String string, int i) {
        // TODO Auto-generated constructor stub
    }

    public void changeName(String name) {
        // TODO Auto-generated method stub
       
    }

    public void changeAge(int age) {
        // TODO Auto-generated method stub
       
    }

}

Ex. 5.12, Ex. 5.14, and Ex. 5.15

 Ex. 5.12, Ex. 5.14, and Ex. 5.15

//*****************************************************
// Ex. 5.12
//*****************************************************

public class ex5point12 {

    public int maxOfTwo (int num1, int num2)
    {
     int result = num1;
     if (num2 > num1)
     result = num2;
     return result;
    }
}

//*****************************************************
// Ex. 5.14
//*****************************************************

public class ex5point14 {

    public boolean evenlyDivisible (int num1, int num2)
    {
     boolean result = false;
     if (num1 != 0 && num2 != 0)
     if (num1 % num2 == 0 || num2 % num1 == 0)
         result = true;
     return result;
    }
}

//*****************************************************
// Ex. 5.15
//*****************************************************

public class ex5point15 {

    public boolean isAlpha (char ch)
    {
     return ( (ch >= 'a' && ch <= 'z') ||
     (ch >= 'A' && ch <= 'Z') );
    }
}

Car.java & CarTest.java

Car.java & CarTest.java



//*********************************************************************
// Car.java
//*********************************************************************
public class Car {
   

    private String model;
    private int yearOfthecar;
    private String make;
    private final int DELTA = 45;
    public Car (String make, String model, int yearOfthecar)
    {
    this.make = make;
    this.model = model;
    this.yearOfthecar = yearOfthecar;
    }
   
   
    public String getMake()
    {
        return make;
    }
   
    public void setMake(String make)
    {
    this.make = make;
    }
   
    public String getModel()
    {
        return model;
    }
   
    public void setModel(String model)
    {
        this.model = model;
    }
   

    public int getYearOfthecar()
    {
        return yearOfthecar;
    }

    public void setYearOfthecar(int yearOfthecar)
    {
       
        this.yearOfthecar = yearOfthecar;
    }
   
   
    public String toString()
    {
        return ("This car is a " + yearOfthecar + " "+ make + " "+model);
    }
   
    public void isAntique ()
    {
        while (2015 - yearOfthecar > DELTA)
            {
            System.out.println("This car is more than 45 years old and it is an antique car!");
            }
       
    }
}

//****************************************************************************************
// CarTest.java
//****************************************************************************************

public class CarTest {

    public static void main(String[] args) {
       
        int DELTA = 45;
        Car car = new Car ("Oodi ", "A4",2000);
       
        System.out.println("The make is "+car.getMake());
        System.out.println("The model is "+car.getModel());
        System.out.println("The year of car is "+car.getYearOfthecar());
       
        car.setYearOfthecar(1700);
        System.out.println("The year of car is "+car.getYearOfthecar());

        if (2015 - car.getYearOfthecar()> DELTA)
       
        System.out.println("This car is more than 45 years old and it is an antique car!");
       
    }

}


Account.java & ManageAccount.java


Account.java & ManageAccount.java

// *******************************************************
// Account.java   
// *******************************************************
import java.text.NumberFormat;
public class Account
{
private double balance;
private String name;
private long acctNum;

public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
}

public void withdraw(double amount)
{
    if (balance >= amount)
        balance -= amount;
    else
        System.out.println("Insufficient funds");
}

public void deposit(double amount)
{
    if (amount > 0)
        balance += amount;
    else
        System.out.println("Amount should be positive.");
}

public double getBalance()
{
    return balance;
}

public String toString()

    NumberFormat fmt = NumberFormat.getCurrencyInstance();
    return (name + "\t" + acctNum + "\t" + fmt.format(balance));
}

public double chargeFee()
{
    balance -= 10;
    return balance;
}

public void changeName(String newName){
name = newName;
    }
}


// ************************************************************
// ManageAccounts.java    
// ************************************************************
public class ManageAccounts{
    public static void main(String[] args)
    {
        Account acct1, acct2;
       
        acct1 = new Account(1000, "Sally", 1111);

        acct2 = new Account(500, "Joe", 2222);

        acct2.deposit(100);

        System.out.println("Balance: $" + acct2.getBalance() );

       
        acct1.withdraw(50);


        System.out.println("Sally's balance is " + acct1.getBalance() );

       
        acct1.chargeFee();
        acct2.chargeFee();

       
        acct2.changeName("Joseph");

       
        System.out.println(acct1.toString() );
        System.out.println(acct2.toString() );
    }
}

Student.java

Student.java

//******************************************************************
// Student.java   
//******************************************************************

import java.util.Scanner;

public class Student {


    String name;
    int test1 = 0, test2 = 0;
    double average;
    Scanner input = new Scanner(System.in);
     public Student(String studentName)

    {
        name = studentName;
        test1 = 0;
        test2 = 0;
        average = 0.0;
    }

    public void inputGrades() {

        System.out.print( "Enter " + name + "'s score for test1: " );

        test1 = input.nextInt();

        System.out.print( "Enter " + name + "'s score for test2: " );

        test2 = input.nextInt();

    }
    public double getAverage() {

        average = (double)( test1 + test2) / 2;

        return average;

    }


    public String getName() {

      return name;

    }

    public static void main(String[] args) {

      Student student1 = new Student( "Mary" );

      Student student2 = new Student( "Mike" );


        student1.inputGrades();


        System.out.println("Average for Mary is: " + student1.getAverage() );


        student2.inputGrades();


        System.out.println("Average for Mike is: " +  student2.getAverage() );

    }

}

Ex. 8.5a, Ex. 8.5b, Ex. 8.5d

Ex 8.5a, Ex. 8.5b, Ex. 8.5d
//***************************************************
// Ex. 8.5a
//***************************************************
public class ex8point5a {
    public static void main(String[] args) {
       
        String names[] = new String[25];
            names[0] = "Michael Jordan";
            names[1] = "Wilt Chamberlain";
            names[2] = "Bill Russell";
            names[3] = "Shaquille O'Neal";
            names[4] = "Oscar Robertson";
            names[5] = "Magic Johnson";
            names[6] = "Kareem Abdul-Jabbar";
            names[7] = "Tim Duncan";
            names[8] = "Larry Bird";
            names[9] = "Kobe Bryant";
            names[10] = "Jerry West";
            names[11] = "Elgin Baylor";
            names[12] = "Hakeem Olajuwon";
            names[13] = "Bob Pettit";
            names[14] = "Moses Malone";
            names[15] = "Julius Erving";
            names[16] = "John Havlicek";
            names[17] = "Karl Malone";
            names[18] = "Isiah Thomas";
            names[19] = "Charles Barkley";
            names[20] = "Rick Barry";
            names[21] = "John Stockton";
            names[22] = "Elvin Hayes";
            names[23] = "Bob Cousy";
            names[24] = "David Robinson";
   
            int size = names.length;
            for (int i=0; i<size; i++)
            {
                System.out.println(names[i]);
           
        }
    }
}

//**************************************************************
// Ex. 8.5b
//**************************************************************

public class ex8point5b {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        double grades[] = new double [41];
        String names[] = new String[40];
       
        names[0] = "Michael Jordan";
        names[1] = "Wilt Chamberlain";
        names[2] = "Bill Russell";
        names[3] = "Shaquille O'Neal";
        names[4] = "Oscar Robertson";
        names[5] = "Magic Johnson";
        names[6] = "Kareem Abdul-Jabbar";
        names[7] = "Tim Duncan";
        names[8] = "Larry Bird";
        names[9] = "Kobe Bryant";
        names[10] = "Jerry West";
        names[11] = "Elgin Baylor";
        names[12] = "Hakeem Olajuwon";
        names[13] = "Bob Pettit";
        names[14] = "Moses Malone";
        names[15] = "Julius Erving";
        names[16] = "John Havlicek";
        names[17] = "Karl Malone";
        names[18] = "Isiah Thomas";
        names[19] = "Charles Barkley";
        names[20] = "Rick Barry";
        names[21] = "John Stockton";
        names[22] = "Elvin Hayes";
        names[23] = "Bob Cousy";
        names[24] = "David Robinson";
        names[25] = "II. Tim Duncan";
        names[26] = "II. Larry Bird";
        names[27] = "II. Kobe Bryant";
        names[28] = "II. Jerry West";
        names[29] = "II. Elgin Baylor";
        names[30] = "II. Hakeem Olajuwon";
        names[31] = "II. Bob Pettit";
        names[32] = "II. Moses Malone";
        names[33] = "II. Julius Erving";
        names[34] = "II. John Havlicek";
        names[35] = "II. Karl Malone";
        names[36] = "II. Isiah Thomas";
        names[37] = "II. Charles Barkley";
        names[38] = "II. Rick Barry";
        names[39] = "II. John Stockton";

       
        grades[1] = 45;
        grades[2] = 67;
        grades[3] = 65;
        grades[4] = 75;
        grades[5] = 54;
        grades[6] = 10;
        grades[7] = 57;
        grades[8] = 86;
        grades[9] = 75;
        grades[10] = 24;
        grades[11] = 75;
        grades[12] = 75;
        grades[13] = 43;
        grades[14] = 32;
        grades[15] = 75;
        grades[16] = 42;
        grades[17] = 54;
        grades[18] = 32;
        grades[19] = 43;
        grades[20] = 32;
        grades[21] = 57;
        grades[22] = 97;
        grades[23] = 65;
        grades[24] = 65;
        grades[25] = 65;
        grades[26] = 75;
        grades[27] = 54;
        grades[28] = 10;
        grades[29] = 57;
        grades[30] = 86;
        grades[31] = 75;
        grades[32] = 24;
        grades[33] = 75;
        grades[34] = 75;
        grades[35] = 43;
        grades[36] = 32;
        grades[37] = 75;
        grades[38] = 65;
        grades[39] = 75;
        grades[40] = 84;
       
        int size = names.length;
        int size1 = grades.length;
        int i1 = 0;
        for (int i=0 ; i<size && i1<size1 ; i++)
        {
            i1++;
         {
            System.out.println(names[i] + "     " + grades[i1]);
       
         }
         }
        }
    }

//*******************************************************
// Ex. 8.5d
//*******************************************************
public class ex8point5d {

     public static void main(String[ ] args)
     {
         int[][] scores = { {1, 36, 43, 54, 75, 78, 46, 33, 72},
                 {2, 82, 44, 73, 24, 83, 33, 62, 81},
                 {3, 33, 45, 54, 65, 75, 33, 52, 85},
                 {4, 61, 17, 71, 43, 81, 42, 51, 73},
                 {5, 53, 64, 85, 52, 71, 64, 33, 62},
                 {6, 32, 34, 53, 64, 73, 83, 92, 51},
                 {7, 43, 65, 64, 75, 85, 43, 22, 65},
                 {8, 61, 61, 51, 43, 31, 72, 51, 83},
                 {9, 53, 35, 44, 65, 85, 53, 32, 55},
                 {10, 61, 61, 81, 55, 41, 32, 21, 63}};
       
          System.out.println("Student      Grades");
         for (int i = 0; i < scores.length; i++)
         {
                System.out.print(scores[ i ] [ 0 ] + ". Student : ");
                for (int j = 1; j < scores[ i ].length; j++)
                {
                           System.out.print(scores[ i ][ j ] + " ");
                }
               System.out.println( );
         }
   }
}



Foo.java Averaging Numbers

 Foo.java for Total and Average of numbers. To run this program go to 'run configuration' Click on 'Arguments' tab... Write any numbers with space between them to get and average of that numbers.

//**********************************************************************
//  Averaging Numbers   Foo.java
//**********************************************************************

public class Foo
{
    public static void main(String args[])
    {
        int sum=0,i,count=0,n;
        float avg;
        if (args.length == 0)
            System.out.println("No arguments");
        else
        {
            i = args.length;
            System.out.println("Number of arguments is " + i);
           
            while(count < i)
            {
                n = Integer.parseInt(args[count]);
                sum += n;
                count += 1;
            }
            avg = sum/i;
            System.out.println("The sum is " +sum);
            System.out.println("The average is " +avg);

        }
    }
}

Sales.java



 Sales.java

//*******************************************************************************************            Sales.java
//*******************************************************************************************
import java.util.Scanner;
public class Sales {
public static void main(String[] args){

    final int SALESPEOPLE = 5;
    int[] sales = new int[SALESPEOPLE];
    int sum, randomValue, numGreater = 0;
    int max = sales[0];
    int min = sales[0];
    int maxperson = 0;
    int minperson = 0;



    Scanner scan = new Scanner(System.in);
    for (int i=0; i<sales.length; i++)
        {
        System.out.print("Enter sales for salesperson " + Integer.valueOf(i+1) + ": ");
        sales[i] = scan.nextInt();

        if(sales[i] > max){
            max= sales[i];
            maxperson = i;
        }
        if(sales[i] < min) {
            min = Integer.MAX_VALUE;
           
            minperson= i;
        }}
   
   
    System.out.println("\nSalesperson   Sales");
    System.out.println("--------------------");
    sum = 0;
    for (int i=0; i<sales.length; i++)
        {
        System.out.println("     " + Integer.valueOf(i+1) + "         " + sales[i]);
        sum += sales[i];
        }

    System.out.println("\nTotal sales: " + sum);
    System.out.println("Average sales: " + sum/5);
    System.out.println();
    System.out.println("Salesperson " + Integer.valueOf(maxperson+1) + " had the most sales with " + max );
    System.out.println("Salesperson " + Integer.valueOf(minperson+1) + " had the least sales with " + min);
    System.out.println();
    System.out.println("Enter any value to compare to the sales team: ");
    randomValue = scan.nextInt();
    System.out.println();
    for(int r=0; r < sales.length; r++){
        if(sales[r] > randomValue){
            numGreater++;
            System.out.println("Salesperson " + Integer.valueOf(r+1) + " exceeded that amount with " + sales[r]);
        }}
    System.out.println();
    System.out.println("In total, " + numGreater + " people exceeded that value");

      } }

Tuesday, February 16, 2016

Programming Project 6.7 a,b,c,d star.java

Programming Project 6.7 a, 6.7 b, 6.7 c, 6.7 d... star.java

===============================================================
//*************************************************************
// stars_a.java
// Programming Project 6.7 a
//*************************************************************
package stars_a;
public class stars_a {

public static void main(String[]args)
{
for (int row = 0; row < 11; ++row)
{
for (int space = 0; space < 11 - row - 1; ++space)
{
System.out.print("*");
}
for (int stars = 10; stars < row + 1; ++stars)
{
System.out.print(" ");
}
System.out.println();
}}}

=============================================================
//**************************************************************
// stars_b.java   
// Programming Project 6.7 b
//**************************************************************
package stars_b;
public class stars_b {

public static void main(String[]args)
{
for (int row = 0; row < 11; ++row) 
{  
for (int space = 0; space < 11 - row; ++space) 
{  
System.out.print(" ");  
}  
for (int stars = 0; stars < row; ++stars) {  
System.out.print("*");  
}  
System.out.println();  
}}}


=============================================================

//************************************************************
// stars_c.java   
// Programming Project 6.7 c
//************************************************************
package stars_c;
public class stars_c {

public static void main(String[]args){
for (int row = 0; row < 10; ++row )
{
for (int spaces = 10; spaces > 10 - row - 1; --spaces)
{
System.out.print(" ");
}
for (int stars = 10; stars > row; --stars)
{
System.out.print("*");
}
System.out.println();
}}}

============================================================

//*************************************************************
// stars_d.java   
// Programming Project 6.7 d
//*************************************************************
package stars_d;
public class stars_d {
public static void main(String[] args) {
    for (int i = 1; i < 11; i += 2) {
      for (int j = 0; j < 10 - i / 2; j++)
        System.out.print(" ");

      for (int j = 0; j < i; j++)
        System.out.print("*");

      System.out.print("\n");
    }

    for (int i = 9; i > 0; i -= 2) {
      for (int j = 0; j < 10 - i / 2; j++)
        System.out.print(" ");

      for (int j = 0; j < i; j++)
        System.out.print("*");

      System.out.print("\n");
    }}}

Rock, Paper, Scissors

Rock.java Rock, Paper Scissors Game



//************************************************************
// Rock.java  Rock, Paper, Scissors Game
//************************************************************
import java.util.Scanner;
import java.util.Random;

public class Rock {
public static void main(String[] args)
{
   String personPlay;
   String computerPlay = "";
   int computerInt;
   String response;
   Scanner scan = new Scanner(System.in);
   Random generator = new Random();

   System.out.println("Hey, let's play Rock, Paper, Scissors!\n" +
   "Please enter a move.\n" + "Rock = R, Paper" +
                      "= P, and Scissors = S.");

   System.out.println();
   computerInt = generator.nextInt(3)+1;

   if (computerInt == 1)
      computerPlay = "R";
   else if (computerInt == 2)
      computerPlay = "P";
   else if (computerInt == 3)
      computerPlay = "S";

   System.out.println("Enter your play: ");
   personPlay = scan.next();

   personPlay = personPlay.toUpperCase();

   System.out.println("Computer play is: " + computerPlay);

   if (personPlay.equals(computerPlay))
      System.out.println("It's a tie!");
   else if (personPlay.equals("R"))
      if (computerPlay.equals("S"))
         System.out.println("Rock crushes scissors. You win!!");
   else if (computerPlay.equals("P"))
           System.out.println("Paper crushes rock. You lose!!");
   else if (personPlay.equals("P"))
      if (computerPlay.equals("S"))
      System.out.println("Scissor cuts paper. You lose!!");
   else if (computerPlay.equals("R"))
           System.out.println("Paper crushes rock. You win!!");
   else if (personPlay.equals("S"))
        if (computerPlay.equals("P"))
        System.out.println("Scissor cuts paper. You win!!");
   else if (computerPlay.equals("R"))
           System.out.println("Rock breaks scissors. You lose!!");
   else
        System.out.println("Invalid user input.");
}}

Ex. 6.2 & Ex. 6.3


Exercise solution Ex. 6.2, Ex. 6.3
=======================================================================
package exercise6_2;

import java.util.Scanner;

//********************************************************************************
// exercise6_2.java          To solve the exercise 6.2
//********************************************************************************

public class exercise6_2 {


public static void main(String[] args)
{
for (int num = 0; num <= 200; num += 2)
System.out.println (num);

}}

=====================================================================

package exercise6_3;

import java.util.Scanner;

//********************************************************************************
// exercise6_3.java          To solve the exercise 6.3
//********************************************************************************

public class exercise6_3 {


public static void main(String[] args)
{
for (int val = 200; val >= 0; val -= 1)
if (val % 4 != 0)
System.out.println (val);

}
         }

==================================================================

Monday, February 15, 2016

Ex 5.3, Ex. 5.4, and Ex. 5.5

Solution for Ex. 5.3, 5.4, and 5.5....


==============================================================

// Exercise Solution 5.3
public class Exe5_3
{
private static final Object MIN_LENGTH = null;

public static void main(String[] args) {
Object length = null;
if (length == MIN_LENGTH)
System.out.println("The length is minimal.");
}
}

==============================================================

// Exercise solution 5.4
public class Exe5_4 {

public static void main(String[] args) {

int num = 87, max = 25;
if (num >= max*2)
System.out.println ("apple");
System.out.println ("orange");
System.out.println ("pear");
}
}

==============================================================

public class Exe5_5 {

public static void main(String[] args) {
int limit = 100, num1 = 15, num2 = 40;
if (limit <= limit)
{
if (num1 == num2)
System.out.println("lemon");
System.out.println("lime");}
System.out.println("grape");
}
}

=============================================================

HiLoGame

HiLoGame.java Guessing Game program using if else and while loop


//*****************************************************************************
// HiLoGame.java  
// Guessing game program using if else and while loop.
//******************************************************************************
import java.util.Scanner;

public class HiLoGame {

   public static void main(String[] args) {
      System.out.println("Welcome to Number Guessing Game, Guess number between 1 to 100");
      Scanner scanner = new Scanner(System.in);
     
      int number= (1 + (int)(Math.random()*100));
      int guess = -1;
     
      while (guess!=number) {
         
         System.out.print("Enter your guess: ");
         
         guess = scanner.nextInt();
                   
         if (guess<number) {
           
            System.out.println("Number is too low, please try again");
           
         } else if (guess>number) {

            System.out.println("Number is too high, please try again");
           
         } else {
           

            System.out.println("Yeppi, good guessing, the number is correct, the number is " + number);
         }
   }}}


Baseballstat

 BaseballStat. java to call the file and print output use the below link to download file to call... 

Click Here 

//************************************************************************************
// BaseballStat.java  
// To call the file and print output
//************************************************************************************


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

public class BaseballStat
{
  public static void main (String[] args) throws IOException
  {
    int hit, out, walk, sacrifice;
    Scanner fileScan, lineScan;
    String fileName;
    String current;
    Scanner scan = new Scanner(System.in);
    System.out.print ("Enter the name of the input file: ");
    fileName = scan.nextLine(); 
    fileScan = new Scanner(new File(fileName));

    while (fileScan.hasNext())
     {
         hit = 0; out = 0; walk = 0; sacrifice = 0;
         String player = fileScan.nextLine();
         System.out.println ("Player: " + player);

         lineScan = new Scanner (player);
         lineScan.useDelimiter(",");           
        
         while (lineScan.hasNext())   
         {
            current = lineScan.next();
            if (current.equals("h"))
                hit++;
           
            if (current.equals( "o"))
                out++;
           
            if (current.equals("w"))
                walk++;
           
            if (current.equals("s"))
                sacrifice++;
        }
        System.out.println("Hits: " + hit + "  Outs: " + out + "  Walks: " + walk + "  Sacrifice: " +sacrifice);
        System.out.println("");  
      }
   
   
  }
}

Saturday, February 13, 2016

ProgrammingProject 3.1

Programming Project 3.1 to generate username for new computer account by sing FN, LN and random number from 00 to 99:

//********************************************************************************
//   PP3_1.java
//   Programming Project 3.1
//   To generate user name for new computer account by using first name, last name
//   and random number from 00 to 99
//*********************************************************************************

import java.util.Scanner;
public class PP3_1
{
 public static void main(String[] args)
 {

 String First_Name;
 String Last_Name;
 String Output;

 Scanner scan = new Scanner(System.in);

 System.out.print("Please Enter First Name: ");
 First_Name=scan.next();

 System.out.print("Please Enter Last Name: ");
 Last_Name=scan.next();

 System.out.println("Name is: "+First_Name+" "+Last_Name);

 int randomNum = 10 + (int)(Math.random()*99);

 Output=First_Name.substring(0,1)+Last_Name.substring(0,5)+randomNum;

 System.out.println("User name is: "+Output);

 }

}

PlayingCard

PlayingCard.java to demonstrates the use of enumerated types using Card Game:

//************************************************************************
//  PlayingCard.java    
//
//  Demonstrates the use of enumerated types using Card Game
//************************************************************************

public class PlayingCards
{

public enum Rank{Joker, Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King};

   public static void main(String[] args)
{
  Rank highCard, faceCard, card1, card2;

  highCard = Rank.Ace;
  faceCard = Rank.Queen;
  card1 = Rank.Two;
  card2 = Rank.Nine;
 
  System.out.println("A blackjack hand: " + highCard + " and " + faceCard);
  System.out.println ();
  System.out.println("card1 name: " + card1);
     System.out.println("card1 face value: " + card1.ordinal());
     System.out.println ();
     System.out.println ("card2 name: " + card2);
     System.out.println ("card2 face value: " + card2.ordinal());
     System.out.println ();
     System.out.println("A two card hand: " + card1 + " and " + card2);
     System.out.println("Hand value: " + (card1.ordinal() + card2.ordinal()));

       }
  }

Distance

Distance.java to compute the distance between two points:

//*************************************************************************
//  Distance.java
//  Computes the distance between two points
//*************************************************************************

import java.util.Scanner;

public class Distance
{

public static void main(String[] args) {

double x1, y1, x2, y2;

Scanner scan = new Scanner(System.in);

System.out.print ("Enter the coordinates of the first point " +
"(put a space between them): ");

x1 = scan.nextDouble();
y1 = scan.nextDouble();

System.out.print ("Enter the coordinates of the second point: ");
x2 = scan.nextDouble();
y2 = scan.nextDouble();

double dist;

dist = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));

System.out.print("Distance between two point is: ");
System.out.println(dist);
}
}

TempConverter

Simple program to convert temperature from Fahrenheit to celcius:

//********************************************************************
// TempConverter.java
// Convert Fahrenheit (°F) to Celcius (°C)
//********************************************************************
import java.util.Scanner;

public class TempConverter
{

public static void main(String[] args)
{
// TODO Auto-generated method stub


Scanner scan = new Scanner (System.in);

System.out.println ("Please enter Fahrenheit (°F) : ");
double fahrenheit;
fahrenheit = scan.nextDouble();

double celsius = (fahrenheit-32) * 5 / 9;

System.out.println ("Celcius (°C) = " + celsius + " °C");


}

}

Exercise 2.11

Exercise 2.11 Solution:

package exercise;

import java.util.Scanner;

//********************************************************************************
// Author: Biren Patel
// exercise.java          To solve the exercise 2.11
//********************************************************************************

public class exercise {


public static void main(String[] args)
{
int iResult, num1 = 25, num2 = 40, num3 = 17, num4 = 5;
double fResult, val1 = 17.0, val2 = 12.78;
System.out.println("answer f: " + (fResult = val1 / val2));
System.out.println("answer g: " + (iResult = num1 / num2));
System.out.println("answer h: " + (fResult = (double) num1 / num2));
System.out.println("answer i: " + (fResult = num1 / (double) num2));
System.out.println("answer j: " + (fResult = (double) (num1 / num2)));
System.out.println("answer k: " + (iResult = (int) (val1 / num4)));
System.out.println("answer l: " + (fResult = (int) (val1 / num4)));
System.out.println("answer m: " + (fResult = (int) ((double) num1 / num2)));
System.out.println("answer n: " + (iResult = num3 % num4));
System.out.println("answer o: " + (iResult = num2 % num3));
System.out.println("answer p: " + (iResult = num3 % num2));
System.out.println("answer q: " + (iResult = num2 % num4));


}

}

BaseConversion.java


1. Base conversion:
Conversion from base 10 to base 2-9
//*************************************************************************
// BaseConversion.java
// Conversion from base 10 to base 2-9
//*************************************************************************


import java.util.Scanner;

public class BaseConversion 
{

public static void main(String[] args) 
{
int base ;
int base10Num;


Scanner scan = new Scanner(System.in);
System.out.println ("Please enter a base (2-9): ");
base = scan.nextInt();
int maxNumber = (base*base*base*base-1);
System.out.println ("The maximum 4-digit number in base " + base +  " is " + maxNumber);
System.out.println ("");
System.out.println ("Please enter a base 10 number in the range 0 to " + (maxNumber) + " to convert: ");
System.out.println ("");
base10Num = scan.nextInt();
int place0 = base10Num % base;
int place1 = (base10Num / base) % base;
int place2 = (base10Num / (base*base)) % base;
int place3 = (base10Num / (base*base*base)) % base;

System.out.println ("");
System.out.println ((base10Num)+ " (base 10) = " +  place3+""+place2+""+place1+""+place0 + " (base " + (base) + ")");
}

}

Java Software Solutions 8th edition by Lewis and Loftus

Enjoy exercise solutions and Programming Project solutions of "Java Software Solutions 8th edition" by Lewis and Loftus...

I will keep posting different programs from the book and lab manual. 

I used "Eclipse" for all the programs...