In: Computer Science
Fundamentals of Programming
USING JAVA
Please copy here your source codes, including your input and output screenshots. Please upload this document along with your source files (i.e., the .java files) on Blackboard by the due date.
1.
(Display three messages) Write a program that displays Welcome to Java, Welcome to Computer Science, and Programming is fun.
2.
(Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the result. One foot is 0.305 meter. Here is a sample run:
Enter a value for feet: 16.5
16.5 feet is 5.0325 meters
3.
(Financial application: compute taxes) Listing 3.5, ComputeTax.java, gives the source code to compute taxes for single filers. Complete this program to compute taxes for all filing statuses.
4.
(Financial application: payroll) Write a program that reads the following information and prints a payroll statement:
Employee’s name (e.g., Smith)
Number of hours worked in a week (e.g., 10)
Hourly pay rate (e.g., 9.75)
Federal tax withholding rate (e.g., 20%)
State tax withholding rate (e.g., 9%)
A sample run is as follows:
Enter employee’s name: Smith
Enter number of hours worked in a week: 10
Enter hourly pay rate: 9.75
Enter federal tax withholding rate: 0.20
Enter state tax withholding rate: 0.09
Employee Name: Smith
Hours worked: 10.0
Pay Rate: $9.75
Gross Pay: $97.5
Deductions:
Federal Withholding (20.0%): $19.5
State Withholding (9.0%): $8.77
Total Deduction: $28.27
Net Pay: $69.22
5.
(Find the largest n such that n3<12,000) Use a while loop to find the largest integer n such that n3 is less than 12,000.
Answer 1.
Here I displayed all three message in three lines.
import java.util.*;
public class Main
{
public static void main(String[] args) {
System.out.println("Welcome to
Java");
System.out.println("Welcome to
Computer Science");
System.out.println("Programming is
fun");
}
}
Output:
Welcome to Java
Welcome to Computer Science
Programming is fun
Answer 2.
Here I convert foot into meters according to questions One foot is equals to 0.305 meters(please check real value of one foot from your side).
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
double mtr=0.305, result;
System.out.print("Enter a value for
feet:");
double ft=sc.nextDouble();
result= ft*mtr;
System.out.println(ft+" feet is
"+result+" meters ");
}
}
Output:
Enter a value for feet: 16.5
16.5 feet is 5.0325 meters
Answer 4:
Here I used varibales name such as
noh : number of hours worked
hpr : hourly pay rate
ftw : federal tax withholding rate
stw : state tax withholding rate
gp : Gross pay
fw : Federal Withholding
sw : State Withholding
td : Total Deduction
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter
employee’s name :");
String name=sc.next();
System.out.println("Enter number of
hours worked in a week :");
int noh=sc.nextInt();
System.out.println("Enter hourly
pay rate :");
double hpr=sc.nextDouble();
System.out.println("Enter federal
tax withholding rate :");
double ftw=sc.nextDouble();
System.out.println("Enter state tax
withholding rate :");
double stw=sc.nextDouble();
System.out.println("Employee Name
:"+name);
System.out.println("Hours worked
:"+noh);
System.out.println("Pay Rate :
$"+hpr);
double gp= noh*hpr;
System.out.println("Gross Pay :
$"+gp);
System.out.println("Deduction
:");
double fw,sw,td;
fw= ((ftw/1)*gp);
sw=((stw/1)*gp);
td= fw+sw;
System.out.println("Federal
Withholding : $"+fw);
System.out.println("State
Withholding : $"+sw);
System.out.println("Total Deduction
: $"+td);
System.out.println("Net Pays:
$"+(gp-td));
}
}
Answer 5 :
import java.util.*;
public class Main
{
public static void main(String[] args) {
int n=0;
while(true) {
if(n*n*n<12000) {
n++;
}
else
break;
}
System.out.println("The largest integer of n less than 12000 is:
"+(n-1));
}
}
Output:
The largest integer of n less than 12000 is: 22