Tuesday, 6 October 2015

cs java programs

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

1. [20 points] Write a program that reads the subtotal and the gratuity rate, then computes the gratuity and total. For example, if the user enters 10 for subtotal and 15% for gratuity rate, the program displays $1.5 as gratuity and $11.5 as total. Here is a
sample run:
2. [20 points] Write a program that receives an ASCII code (an
integer between 0 and 127) and displays its character. For
example, if the user enters 97, the program displays character a.
Here is a sample run:
Enter an ASCII code : 69
The character is E Enter the subtotal and a gratuity rate:
10
15
The gratuity is $1.5 and total is $11.5
3. [20 pts] Write a program that prompts the user to enter an
integer. If the number is a multiple of 5, print HiFive. If the
number is divisible by 2 or 3, print Georgia. Here are the sample
runs:
<Output>
Enter an integer: 6
Georgia
<End Output>
<Output>
Enter an integer: 15
HiFive Georgia
<End Output>
<Output>
Enter an integer: 25
HiFive
<End Output>
4. [ 25 points] Write a program that prompts the user to enter an integer for today’s day of the week(Sunday is 0, Monday- 1,…….Saturday-6). Also prompt the user to enter the number of
days after today for a future day and display the future day of the week.
Hint: if today is Sunday, we would enter 0 and we want to know
what day it is after 10 days.We can do this by:
(0+10)%7 =3 which implies that it would be Wednesday after 10
days.
Here is a sample run:
Enter today’s day : 1
Enter the number of days elapsed since today : 17
Today is Monday and the future day is Thursday.
5.[15 points] Subraction Quiz
Write a program to teach a first grade child how to learn
subtractions. The program randomly generates two single-digit
integers number1 and number2 with number1 >= number2 and
displays a question such as “What is 9 – 2?” to the student. After
the student types the answer, the program displays whether the
answer is correct.




answers:



1)Write a program that reads the subtotal and thegratuity rate, then computes the gratuity and total?
/*
* 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 classweek1;
/**
*
* @author kishu
*/
import java.util.*;
public class Gratuity
{
public static void main(String[] ar)
{
Scanner sc = new Scanner(System.in);
double subtotal, gratuityRate, total, gratuity;
System.out.print("Enter a value for subtotal:");
subtotal = sc.nextDouble();
System.out.print("Enter a value for gratuity rate:");
gratuityRate = sc.nextDouble();
gratuity = (subtotal * gratuityRate) / 100;
total = subtotal + gratuity;
System.out.print("The gratuity is $" + gratuity + " and total is $" + total + ".");
}
}
Out put:
Enter a value for subtotal:10 15
Enter a value for gratuity rate:The gratuity is $1.5 and total is $11.5

2)Write a program that receives an ASCII code (an integer between 0 and 127) and displays its character.
/*
* 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 classweek1;
/**
*
* @author kishu
*/
import java.util.*;
public class ASCIIcode {
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter number between 0 to 127 ");
int i = sc.nextInt();
char c = (char)i;
System.out.println("charecter is="+c);
}
}
Output:
enter number between 0 to 127
65charecter is=A
3) Write a program that prompts the user to enter an integer. If the number is a multiple of 5, print HiFive. If thenumber is divisible by 2 or 3, print Georgia.
/*
* 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 classweek1;
/**
*
* @author kishu
*/
import java.util.Scanner;
public class MulDiv {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = input.nextInt();
if (num % 5 == 0)
{
System.out.println("HiFive");
CS480_HW1_17464_Kishore_Elchuri
}
else if (num % 2 == 0|| num% 3 == 0)
{
System.out.println("Georgia");
}
else if ((num % 5 == 0)&& (num % 2 == 0 || num % 3 == 0))
{
System.out.println("HiFive Georgia");
}
}
}
Output:
Enter an integer: 26
Georgia

4) Write a program that prompts the user to enter an integer for today’s day of the week(Sunday is 0, Monday-1,…….Saturday-6). Also prompt the user to enter the number of days after today for a future day and display the future day of the week.
/*
* 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 classweek1;
/**
*
* @author kishu
*/
import java.util.*;
public class WeekProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int today, elapsedDays;
int daysToAdd, dayToFind;
System.out.print("Enter today's day:");
today = sc.nextInt();
System.out.print("Enter the number of days elapsed since today:");
elapsedDays = sc.nextInt();
daysToAdd = elapsedDays % 7;
dayToFind = today + daysToAdd;
CS480_HW1_17464_Kishore_Elchuri
if(today == 0) {
System.out.print("To day is Sunday");
} else if(today == 1) {
System.out.print("To day is Monday");
} else if(today == 2) {
System.out.print("To day is Tuesday");
} else if(today == 3) {
System.out.print("To day is Wednesday");
} else if(today == 4) {
System.out.print("To day is Thursday");
} else if(today == 5) {
System.out.print("To day is Friday");
} else if(today == 6) {
System.out.print("To day is Saturday");
}
if(dayToFind == 0) {
System.out.print(" and the future day is Sunday.");
} else if(dayToFind == 1) {
System.out.print(" and the future day is Monday.");
} else if(dayToFind == 2) {
System.out.print(" and the future day is Tuesday.");
} else if(dayToFind == 3) {
System.out.print(" and the future day is Wednesday.");
} else if(dayToFind == 4) {
System.out.print(" and the future day is Thursday.");
} else if(dayToFind == 5) {
System.out.print(" and the future day is Friday.");
} else if(dayToFind == 6) {
System.out.print(" and the future day is Saturday.");
}
}
}
Output :
Enter today's day:1
Enter the number of days elapsed since today:17
To day is Monday and the future day is Thursday

5) Write a program to teach a first grade child how to learnsubtractions. The program randomly generates two single-digit integers number1 and number2 with number1 >= number2 and displays a question such as “What is 9 – 2?” to the student. After
the student types the answer, the program displays whether the answer is correct.
/*
* 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 classweek1;
/**
*
* @author kishu
*/
import java.util.*;
CS480_HW1_17464_Kishore_Elchuri
public class SubtractionQuiz {
public static void main(String ar[])
{
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);
if (number1 < number2) {
int temp = number1;
number1 = number2;
number2 = temp;
}
System.out.print("What is " + number1 + " - " + number2 + "? ");
Scanner input = new Scanner(System.in);
int answer = input.nextInt();
if (number1 - number2 == answer)
System.out.println("You are correct");
else
System.out.println("Your answer is wrong.\n" + number1 + " - "
+ number2 + " should be " + (number1 - number2));
}
}
Output :
What is 7 - 5? 2
You are correct

No comments:

Post a Comment