10
COMPUTER PROGRAMMING Lab(5)

COMPUTER PROGRAMMING Lab(5). Exercise 1 (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the

Embed Size (px)

Citation preview

Page 1: COMPUTER PROGRAMMING Lab(5). Exercise 1 (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the

COMPUTER PROGRAMMINGLab(5)

Page 2: COMPUTER PROGRAMMING Lab(5). Exercise 1 (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the

Exercise 1

(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:

Page 3: COMPUTER PROGRAMMING Lab(5). Exercise 1 (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the

Source code:

package ex2_3;

import java.util.Scanner;public class EX2_3 {

public static void main(String[] args) {Scanner input = new Scanner (System.in); System.out.println("Enter a value in feet : "); double feet = input.nextDouble(); double meter = feet *0.305 ; System.out.println(feet+ " Feet is " + meter + " meter."); }}

Page 4: COMPUTER PROGRAMMING Lab(5). Exercise 1 (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the

Output:

Page 5: COMPUTER PROGRAMMING Lab(5). Exercise 1 (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the

Exercise 2

(Financial application: calculate tips) 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 the subtotal and 15% for gratuity rate, the program displays $1.5 as gratuity and $11.5 as total. Here is a sample run :

Page 6: COMPUTER PROGRAMMING Lab(5). Exercise 1 (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the

Source code:package ex2_5;import java.util.Scanner;public class EX2_5 {public static void main(String[] args) { // Read subtotal Scanner input = new java.util.Scanner(System.in); System.out.print("Enter subtotal and gratuity rate: "); double subtotal = input.nextDouble(); double rate = input.nextDouble(); double gratuity = subtotal * rate / 100; double total = subtotal + gratuity; System.out.println ("The gratuity is " + gratuity + " total is " + total); }}

Page 7: COMPUTER PROGRAMMING Lab(5). Exercise 1 (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the

Output:

Page 8: COMPUTER PROGRAMMING Lab(5). Exercise 1 (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the

Exercise 3

(Swap two numbers) Write a program that swap two integers then displays them before and after swapping.

Page 9: COMPUTER PROGRAMMING Lab(5). Exercise 1 (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the

Output:

Page 10: COMPUTER PROGRAMMING Lab(5). Exercise 1 (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the

public class EX1 {

public static void main(String[] args) { int num1 = 10; int num2 = 20; System.out.println("Before Swapping"); System.out.println("Value of num1 is :" + num1); System.out.println("Value of num2 is :" +num2); //swap the value

int temp = num1; num1 = num2; num2 = temp; System.out.println("After Swapping"); System.out.println("Value of num1 is :" + num1); System.out.println("Value of num2 is :" +num2); }}

Source code: