Tuesday, 6 October 2015

cs java progrmes

                                      CS480 – Java and Internet Applications Homework 2 (Fall 2015)

1. [25 points] Write a program that plays the popular scissorrock-
paper game.( A scissor can cut paper, a rock can knock a
scissor and a paper can wrap a rock). The program randomly
generates a number 0,1 or 2 representing scissor, rock and
paper. The program prompts the user to enter a number 0,1 or
2 and displays a message indicating whether the user or the
computer wins, loses or draws. Here are sample runs:
Scissor
(0),
rock
(1),
paper
(2)
:
1
The
computer
is
scissor,
You
are
rock.
You
won.
Scissor
(0),
rock
(1),
paper
(2)
:
2
The
computer
is
paper,
You
are
paper
too.
It
is
a
draw.
2. [20 points] Write a program that reads three edges for a
triangle and computes the perimeter if the input is valid.
Otherwise display that the input is invalid. The input is valid if
the sum of every pair of two edges is greater than the
remaining edge.
3. [20 pts] Write a program that reads an unspecified number of
integers, determines how many positive and negative values
have been read, and computes the total and average of the input
values. Your program ends with the input 0. Display the
average as a floating point number. Here are sample runs:
Enter integers (input ends if it is 0): 1 2 2 -2 1 0
The number of positives is 4
The number of negatives is 1
The total is 4
The average is 0.8
Enter integers (input ends if it is 0): 0
No numbers are entered except 0
4. [ 20 points] Write a program that prompts the user to enter the
number of students and each student’s name and score.
Display the student name with the highest score.
5. [15 points] Modify program 4 to display the student with
highest score and second highest score with their names.
Here is a sample run:
Enter the number of students: 5
Enter a student name: Alex
Enter a student score: 25
Enter a student name: Bobby
Enter a student score: 30
Enter a student name: Carmen
Enter a student score: 15
Enter a student name: Denis
Enter a student score: 44
Enter a student name: Emma
Enter a student score: 20
Top two students:
Denis's score is 44.0
Bobby's score is 30.0




answers:


1. Write a program that plays the popular scissor rock-paper game.( A scissor can cut paper, a rock can knock a scissor and a paper can wrap a rock). The program randomly generates a number 0,1or2representing scissor, rock and paper. The program prompts the user to enter a number 0,1 or 2 and displays a message indicating whether the user or the computer wins, loses or draws.
program:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clshm2;
/**
*
* @author kishore
*/
import java.util.*;
public class Scissor_Rock_Paper_Game {
public static void main(String[] args)
{

Scanner sc = new Scanner(System.in);
int computer, human;
System.out.print("scissor (0), rock (1), paper (2):");
human = sc.nextInt();
computer = (int)(Math.random() * 3);
if(computer == 0)
{
System.out.print("The computer is scissor");
}
else if(computer == 1)
{
System.out.print("The computer is rock");
}
else if(computer == 2)
{
System.out.print("The computer is paper");
}
if((human == 0 && computer == 2) || (human == 1 && computer == 0) || (human == 2 && computer == 1))
{
if(human == 0)
{
System.out.print(" You are scissor You won");
}
else if(human == 1)
{
System.out.print(" You are rock You won");
}
CS480(N)_HW2_17464_KISHORE_ELCHURI
else if(human == 2)
{
System.out.print(" You are paper You won");
}
}
else if(human == computer)
{
if(human == 0)
{
System.out.print(" You are scissor too It is a draw");
}
else if(human == 1)
{
System.out.print(" You are rock too It is a draw");
}
else if(human == 2)
{
System.out.print(" You are paper too It is a draw");
}
}
else
{
if(human == 0)
{
System.out.print(" You are scissor You lose");
}
else if(human == 1)
{
System.out.print(" You are rock You lose");
}
else if(human == 2)
CS480(N)_HW2_17464_KISHORE_ELCHURI
{
System.out.print(" You are paper You lose");
}
}
}
}
Output:
sample1:
scissor (0), rock (1), paper (2):1
The computer is scissor You are rock You won
sample2:
scissor (0), rock (1), paper (2):3
Invalid choice please enter 0 or 1 or 2 and try
CS480(N)_HW2_17464_KISHORE_ELCHURI
CS480(N)_HW2_17464_KISHORE_ELCHURI
2.Write a program that reads three edges for a triangle and computes the perimeter if the input is valid. Otherwise display that the input is invalid. The input is valid if the sum of every pair of two edges is greater than the remaining edge.
Program:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clshm2;
/**
*
* @author kishore
*/
import java.util.*;
public class Perimeter {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
double side1, side2, side3;
double perimeter;
System.out.print("Enter the 3 sides of the rectangle:");
CS480(N)_HW2_17464_KISHORE_ELCHURI
side1 = s.nextDouble();
side2 = s.nextDouble();
side3 = s.nextDouble();
if((side1 + side3 >= side2) && (side1 + side2 >= side3) && (side2 + side3 >= side1)) {
perimeter = side1 + side2 + side3;
System.out.print("You entered valid values ");
System.out.println("The perimeter of the triangle is " + perimeter);
}
else {
System.out.println("You entered Invalid values ");
}
}
}
Output:
Enter the 3 sides of the rectangle:4
9
6
You entered valid values The perimeter of the triangle is 19.0
BUILD SUCCESSFUL (total time: 7 seconds)
CS480(N)_HW2_17464_KISHORE_ELCHURI
CS480(N)_HW2_17464_KISHORE_ELCHURI
3. Write a program that reads an unspecified number of integers,
determines how many positive and negative values have been read, and computes the total and average of the input values. Your program ends with the input 0. Display the average as a floating point number.
Program:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clshm2;
/**
*
* @author kishore
*/
import java.util.*;
public class TotalAndAverage {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
CS480(N)_HW2_17464_KISHORE_ELCHURI
int positive = 0, negative = 0, total = 0, count = 0;
double average;
System.out.println("Enter the number: ");
int number;
while ((number = input.nextInt()) != 0) {
total += number;
count++;
if (number > 0) {
positive++;
}
else if (number < 0) {
negative++;
}
}
average = (double)total / count;
System.out.println("The no of positive number is " + positive);
System.out.println("The number of negatives is " + negative);
System.out.println("The total is " + total);
System.out.printf("The average is %f ", average);
}
}
Output:
Enter the number:
3
CS480(N)_HW2_17464_KISHORE_ELCHURI
4
8
-1
-3
0
The no of positive number is 3
The number of negatives is 2
The total is 11
The average is 2.200000 BUILD SUCCESSFUL (total time: 13 seconds)
CS480(N)_HW2_17464_KISHORE_ELCHURI
4. [ 20 points] Write a program that prompts the user to enter the
number of students and each student’s name and score. Display the student name with the highest score.
Program:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clshm2;
/**
*
* @author kishore
*/
import java.util.*;
public class StudentScore {
public static void main(String[] args) {
String studentName="", highName="", secondHighName="";
int score=0, highScore=0, secondHighScore=0;
int classSize;
Scanner scan = new Scanner(System.in);
System.out.println("How many students grades do you want to enter? ");
CS480(N)_HW2_17464_KISHORE_ELCHURI
classSize = scan.nextInt();
for (int i = 0; i < classSize; i++) {
System.out.println("Please enter the student #" + (i + 1) + "'s name? ");
scan = new Scanner(System.in);
studentName = scan.nextLine();
System.out.println("Please enter the student #" + (i + 1) + " score? ");
score = scan.nextInt();
if(score >= highScore){
secondHighName = highName;
secondHighScore = highScore;
highName = studentName;
highScore = score;
} else if(score >= secondHighScore && score < highScore){
secondHighName = studentName;
secondHighScore = score;
}
}
scan.close();
System.out.println("Student with the highest score: " + highName + " " + highScore);
System.out.println("Student with the second highest score: " + secondHighName + " " + secondHighScore);
}
}
CS480(N)_HW2_17464_KISHORE_ELCHURI
Output:
How many students grades do you want to enter?
3
Please enter the student #1's name?
narendra
Please enter the student #1 score?
99
Please enter the student #2's name?
chandu
Please enter the student #2 score?
98
Please enter the student #3's name?
kishu
Please enter the student #3 score?
97
Student with the highest score: narendra 99
Student with the second highest score: chandu 98
BUILD SUCCESSFUL (total time: 22 seconds)
CS480(N)_HW2_17464_KISHORE_ELCHURI
CS480(N)_HW2_17464_KISHORE_ELCHURI
5. [15 points] Modify program 4 to display the student with highest score and second highest score with their names.
Program:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clshm2;
import java.util.Scanner;
/**
*
* @author kishore
*/
import java.util.*;
public class DisplayStudentScore {
public static void main(String[] args) {
String studentName="", highName="", secondHighName="";
int score=0, highScore=0, secondHighScore=0;
int classSize;
Scanner scan = new Scanner(System.in);
CS480(N)_HW2_17464_KISHORE_ELCHURI
System.out.println("enter the number of students:");
classSize = scan.nextInt();
for (int i = 0; i < classSize; i++) {
System.out.println("enter a student name :");
scan = new Scanner(System.in);
studentName = scan.nextLine();
System.out.println("enter a student score:");
score = scan.nextInt();
if(score >= highScore){
secondHighName = highName;
secondHighScore = highScore;
highName = studentName;
highScore = score;
} else if(score >= secondHighScore && score < highScore){
secondHighName = studentName;
secondHighScore = score;
}
}
scan.close();
System.out.println("Top Two Students");
System.out.println(highName + "'s score is " + highScore);
System.out.println(secondHighName + "'s score is " + secondHighScore);
}
}
Output:
CS480(N)_HW2_17464_KISHORE_ELCHURI
enter the number of students:
3
enter a student name :
pavan
enter a student score:
97
enter a student name :
venky
enter a student score:
98
enter a student name :
ramesh
enter a student score:
99
Top Two Students
ramesh's score is 99
venky's score is 98
BUILD SUCCESSFUL (total time: 48 seconds)
CS480(N)_HW2_17464_KISHORE_ELCHURI

No comments:

Post a Comment