b
Tuesday, February 23, 2016
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_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.");
}}
//************************************************************
// 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.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);
}
}}}
//*****************************************************************************
// 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("");
}
}
}
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("");
}
}
}
Subscribe to:
Posts (Atom)