You are on page 1of 2

[] J A V A

LAB

JavaLab_2
%Lab2_1:

INPUTS

To accept inputs from keyboard.

Write a program to convert the inches into centimeters.

import java.util.Scanner;
public class Reading1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); double inches = scan.nextDouble(); double centimeters = inches * 2.54; System.out.println(inches +"in =" + centimeters + "cm"); } }

%Lab2_2:
To find the sum of two integers.

import javax.swing.JOptionPane ;
public class example { public static void main(String[] args) {

String first , second;


int A, B , sum ; first = JOptionPane.showInputDialog("Enter first number "); A = Integer.parseInt(first); second = JOptionPane.showInputDialog("Enter second number "); B = Integer.parseInt(second); sum = A + B ;

JOptionPane.showMessageDialog(null, " SUM is " + sum, "RESULTS",


System.exit(0); } }
QUESTION_MESSAGE WARNING_MESSAGE ERROR_MESSAGE INFORMATION_MESSAGE

JOptionPane.PLAIN_MESSAGE);

------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl 1

[] J A V A

LAB

%Lab2_3:
To find the sum from 1 to n import javax.swing.JOptionPane; public class T2 { public static void main(String[] args) { String S; int A , sum = 0; S = JOptionPane.showInputDialog( "Enter integer : " ); A = Integer.parseInt( S ); for (int i=1 ; i<= A; i++) sum = sum + i ; JOptionPane.showMessageDialog( null, "1 + 2 + 3 +..+ "+A +"="+ sum, "summation", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } }

%Lab2_4:
Write a program to convert F into its equivalent centigrade temperature. import javax.swing.JOptionPane; public class temp { public static void main(String[] args) { String S; double centi, fht ; S = JOptionPane.showInputDialog( "Enter integer : " ); fht = Integer.parseDouble( S ); centi = (fht 32 ) * 5 / 9 ; JOptionPane.showMessageDialog( null, fht +" F = " + centi + degree, "Temperature", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); System.out.println("Centigrade equivalent to 69 F is " + centi + " C"); } }

Try this
Write a program to take input and calculate interest rate (10%) for giving loan. Write a program to read the radius and length of a cylinder and computes its volume.
------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl 2

You might also like