0% found this document useful (0 votes)
30 views21 pages

gr8 Part2javaprogramws2425answerkey

Uploaded by

nannuirene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views21 pages

gr8 Part2javaprogramws2425answerkey

Uploaded by

nannuirene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

INTRODUCTION TO JAVA

PROGRAMS WORKSHEET-PART- 2/ANSWER KEY

PROGRAMS ON DATA TYPES

1.Write a program using appropriate data types and print them in the format as shown in the screenshot
below.

Code:
public class transport
{
public static void main(String args[])
{
String nameofthebus="National Transport Service";
String nameofpassenger="Mrs.Jane grey";
int busno=45902565;
int price=1000;
String seat="23";
String from="Chennai";
String to="Bangalore";
String date="05/07/2023";
int age=30;
float boardingtime=05.00f;
System.out.println ( " \t BUS TICKET ");
System.out.println("\n=========================================");
System.out.println("Name of the Bus:\t” +nameofthebus);
System.out.println("Train No:\t \t \t” +busno);
System.out.println("\n---------------------------------------");
System.out.println("From\t \t To ");
System.out.println(""+from+"\t \t \t "+to);
System.out.println("Seat\t \t \t "+seat);
System.out.println("Date\t \t \t Time(in pm) ");
System.out.println(""+date+"\t \t "+boardingtime);
System.out.println("Price\t \t \t "+price);
System.out.println("\n---------------------------------------");
System.out.println("PASSENGER DETAILS: ");
System.out.println("\n---------------------------------------");
System.out.println("Name:" +nameofpassenger);
System.out.println("Age:"+age);
}
}
2.Write a program using appropriate data types and print them in the format as shown in
the screenshot below.

Code:
public class train
{
public static void main(String args[])
{
String nameofthetrain="Shatabdi Train";
String nameofpassenger="Mr.Tripathi";
String coach="AC";
int trainno=12345;
char clas='B';
String seat="23";
String from="Chennai";
String to="Bangalore";
String date="5/05/2023";
int age=30;
float boardingtime=05.00f;
System.out.println ( " \t TRAIN TICKET\t ");
System.out.println("\n=========================================");
System.out.println("Name of the Train:\t \t” +nameofthetrain);
System.out.println("Coach:\t \t \t \t “+coach);
System.out.println("Train No:\t \t \t \t” +trainno);
System.out.println("\n---------------------------------------");
System.out.println("From\t \t To ");
System.out.println(""+from+"\t \t "+to);
System.out.println("Class\t \t \t Seat ");
System.out.println(""+clas+"\t \t \t "+seat);
System.out.println("Date\t \t \t Time(in pm) ");
System.out.println(""+date+"\t \t "+boardingtime);
System.out.println("\n---------------------------------------");
System.out.println("PASSENGER DETAILS: ");
System.out.println("\n---------------------------------------");
System.out.println("Name:" +nameofpassenger);
System.out.println("Age:"+age);
}
}

3.Write a program using appropriate data types and print them in the format as shown in the screenshot
below.

Code:
public class flight
{
public static void main(String args[])
{
String nameoftheflight="British Airways";
String nameofpassenger="JOHN DOE";
String carrier="AC";
String flightno="AC 2505";
char clas='B';
String from="New Delhi D E L";
String to="Los Angeles K L A X";
String date="05/07/2023";
int age=30;
String seat="5A";
float boardingtime=07.45f;
System.out.println ( "\t BOARDING PASS ");
System.out.println("\n=========================================");
System.out.println("Name of the Flight:\t\t\t”+nameoftheflight);
System.out.println("Carrier:\t\t\t\t”+carrier);
System.out.println("FLIGHT No:\t\t\t\t”+flightno);
System.out.println("\n---------------------------------------");
System.out.println("From\t\t\tTo ");
System.out.println(""+from+"\t\t"+to);
System.out.println("Class\t\t\tSeat ");
System.out.println(""+clas+"\t\t\t"+seat);
System.out.println("Date\t\t\tTime ");
System.out.println(""+date+"\t\t"+boardingtime);
System.out.println("\n---------------------------------------");
System.out.println("PASSENGER DETAILS: ");
System.out.println("\n---------------------------------------");
System.out.println("Name:" +nameofpassenger);
System.out.println("Age:"+age);

}
}

PROGRAMS IN JAVA WITHOUT USING SCANNER METHOD

4.Write a program to do various types of arithmetic calculations using different operators.

OUTPUT

Code:
import java.util.*;
public class ArithmeticOperators
{
public static void main(String[] args)
{
int num1 = 10, num2 = 20;
int res;
res = num1+num2;
System.out.println("Addition of " +num1+" and "+num2+" is "+res);
res = num1-num2;
System.out.println("Subtraction of " +num1+" and "+num2+" is "+res);
res = num1*num2;
System.out.println("Multiplication of " +num1+" and "+num2+" is "+res);
res = num2/num1;
System.out.println("Quotient of " +num2+" and "+num1+" is "+res);
}
}
5. Write a program to calculate the area of a rectangle with width= 8,length=6.
Code:
public class rectangle
{
public static void main(String[] args)
{
int w=8,l=6;
float area=w*l;
System.out.println("Area of Rectangle is :"+area);
}
}
OUTPUT

6.Simple program illustrating boolean datatype.

Code:
public class BooleanExample2 {
public static void main(String[] args) {
boolean b1=true;
boolean b2=false;
boolean b3=(b1==b2);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
}
}

PROGRAMS USING SCANNER CLASS


7. Write a program to accept the text as “Introduction to Java Programming Language” and display it as
shown in the below format

Code:
import java.util.*;
public class ScannerExample {
public static void main(String args[]){
Scanner obj = new Scanner(System.in);
System.out.print("Enter the text to be displayed: ");
String name = obj.nextLine();
System.out.println("text is: " + name);

}
}

OUTPUT

8.Program to accept your name and hobby from the user and print it as shown in the screenshot below.

OUTPUT

Code:
import java.util.*;
public class hobby
{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter your name");
String name=in.nextLine();
System.out.println("Enter Your Hobby");
String hobbies=in.nextLine();
System.out.println("My name is "+name);
System.out.println("My hobby "+hobbies);
}
}

9. Write a program to accept the Name, Age,Grade,Section and field of interest from the user and display
in the given format below.

OUTPUT

Code:
import java.util.*;
public class studentinformation {
public static void main(String args[])
{

Scanner SC=new Scanner(System.in);


System.out.println("Enter the name");
String name=SC.nextLine();
System.out.println("Enter the age");
int age=SC.nextInt();
System.out.println("Enter the your interest");
String interest=SC.nextLine();
System.out.println("Enter the grade");
int grade=SC.nextInt();
System.out.println("Enter the sec");
char sec=SC.next().charAt(0);
System.out.println("\n \n Hey ,My name is" + name +" and I am "+age+" years old.I study in
"+grade+sec+" My area of interest is:"+interest);
}
}

10.Program to accept two integer values from the user to calculate Sum,difference,Product,Quotient and
display it as shown in the below format.

OUTPUT

Code:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Type a number");
int num1=in.nextInt();
System.out.println("Type another number");
int num2=in.nextInt();
System.out.println("Sum is:"+(num1+num2));
System.out.println("Difference is:"+(num1-num2));
System.out.println("Product is:"+(num1*num2));
System.out.println("Quotient is:"+(num1/num2));
}
}

11.Write program to calculate and display the gross salary of an employee by adding basic pay, (HRA
(House Rent Allowance) and Conveyance Allowance.

Output:

Code:
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
double HRA,CA,basic,gross;
Scanner sc=new Scanner(System.in);
System.out.println("enter Basic salary ");
basic=sc.nextDouble();
System.out.println("enter HRA ");
HRA=sc.nextDouble();
System.out.println("enter CA ");
CA=sc.nextDouble();
gross=basic+HRA+CA;
System.out.println("Gross Salary="+gross);
}
}
12. Write a program to accept the Principal (P) in Rupees and Rate of Interest in percentage (R) and Time
Period (T) in years as shown in the format below. Formula to calculate SI= PTR/100

Output:

Code:

import java.util.*;
public class SI
{
public static void main(String args[])
{
int p,t;
float r,si;
Scanner SC=new Scanner(System.in);
System.out.println("Enter the Principle Amount (in rupees)");
p=SC.nextInt();
System.out.println("Enter the Rate Of Interest (in percentage)");
r=SC.nextFloat();
System.out.println("Enter the Time Period (in years)");
t=SC.nextInt();
si=(p*t*r)/100;
System.out.println("\n \n The Simple Interest is :" +si+ "Rupees");
}
}

13. Program to accept temperature in Fahrenheit(F) and convert it to Celsius using the Formula: C =0.55
x
(F-32) and display it as shown in the below format.
OUTPUT

Code:
import java.util.*;
public class temperature {
public static void main(String args[])
{

double f,c;
Scanner SC=new Scanner(System.in);
System.out.println("Enter the fahrenheit ");
f=SC.nextDouble();
c=(f-32)*(0.55);
System.out.println("The temperature in Fahrenheit is " +f+ " degree. ");
System.out.print("After converting Fahrenheit to Celsius, ");
System.out.print("the temperature is " +c+ " degree Celsius ");
}
}

14. Program to accept length, width,and height of a Rectangular prism and find its volume in cubic cms
and Surface area in sq.cms and display the output as shown in the below format.
Formula are: Volume=lwh; Surface area=2(wl+hl+hw)

Output:
Code:
import java.util.*;
public class rectangle
{
public static void main(String args[])
{
int l,w,h;
Scanner SC=new Scanner(System.in);
System.out.println("Enter the length");
l=SC.nextInt();
System.out.println("Enter the width");
w=SC.nextInt();
System.out.println("Enter the height");
h=SC.nextInt();
float volume=w*l*h;
double surface=2*(w*l+h*l+h*w);
System.out.println(" =======================");
System.out.println(" Rectangular Prism");
System.out.println(" =======================");
System.out.println("The length of the Rectangular prism is :"+l+"cms");
System.out.println("The width of the Rectangular prism is :"+w+"cms");
System.out.println("The height of the Rectangular prism is :"+h+"cms");
System.out.println("************************************************* ******");
System.out.println("The Volume of Rectangular Prism is:"+volume+"cubic cms");
System.out.println("The Surface Area of Rectangular Prism is:"+surface+"sq. cms");
System.out.println("************************************************* *******");
}
}

15.Program to print square and cube of a number given by the user as shown in the screenshot below.

OUTPUT

Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter any number");
int x=in.nextInt();
System.out.println("Square of a number is= "+(x*x));
System.out.println("Cube of a number is = "+(x*x*x));
}
}

16. Write a program to assign the value of a as 10 and b as 12.Use pre-increment and post-increment
operators on a and b and and print the new values of a and b and display the output as shown in the
below format.
Output:

Code:
public class inc
{
public static void main(String[] args)
{
// declare variables
int a = 10, b = 12;
int result1, result2;
System.out.println("Value of a: " + a);
result1 = ++a;
System.out.println("After pre-increment: " + result1);
System.out.println("Value of b: " + b);
result2 = b++;
System.out.println("After post-increment: " + result2);
}
}
17. Write a program to assign the value of c as 10 and d =14 and use pre-decrement and post-decrement

operators on x and print the new values of c and d

Output:
Code:

public class dec


{
public static void main(String[] args)
{

// declare variables
int c = 10, d = 14;
int result1, result2;
System.out.println("Value of c: " + c);
result1 = --c;
System.out.println("After pre-decrement: " + result1);
System.out.println("Value of d: " + d);
result2 = d--;
System.out.println("After post-decrement: " + result2);

}
}

18.Program to find if two numbers are the same or not using a ternary operator by assigning num1=10,
num2=20 and display the output as shown in the screenshot below.

OUTPUT

Code:
public class program1
{
public static void main(String args[])
{
int num1=10, num2=20;
String result=(num1==num2) ?"Same": "Not same";
System.out.println(result);
}
}
19.Write a program to find if the number entered by the user is a factor of 5 or not using ONLY Ternary
Operator.
OUTPUT

Code:

import java.util.Scanner;
public class factor
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
String c = ((number % 5 ==0))? "Factor of 5" : "Not a Factor of 5";
System.out.println(number+ " is a " +c);
}
}

20.Write a java program to find if the triangle is right angled triangle by the sides entered by the
user using ONLY Ternary Operator.[Hint - Base2 +Perpendicular2 = Hypotenuse2 ]

OUTPUT

Code:
import java.util.Scanner;
public class rttriangle
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the base(in cm): ");
int s1 = sc.nextInt();
System.out.print("Enter the perpendicular(in cm): ");
int s2 = sc.nextInt();
System.out.print(" Enter the Hypotenuse(in cm): ");
int s3 = sc.nextInt();
String c = ((s1*s1)+(s2*s2)==(s3*s3))? " a Right Angled Triangle" : "Not a Right Angled
Triangle";
System.out.println("The triangle is " +c+ " ,where base= " +s1+"cm Perpendicular= " +s2+"cm
and Hypotenuse = " +s3+"cm");
}
}

21.Debug the following code snippets and write the correct code in the blanks provided.

a. class program1
{
public static void main(String args[])
{
int a == 40, b == 60;
int Sum = a + b;
System.out.println( "Sum of variables is "+ sum);
}
}
Ans : int a= 40,b=60
+Sum
b. public class program
{
public static void main(String args[])
{
int a,b,sum,subtr;
a=100;
b=20;
sum1 = a+b;
substr= a-b;
System.out.println ("a+b"+""+"a-b");
System.out.println (sum+""+subtr);
}
}

Ans:sum

c. import java.util *;
public class Main
{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter the integer value of x");
int x=sc.nextInt();
System.out.println("The value of x is:"+X);
}
}
Ans.
import java.util.*;
int x=in.nextInt();
System.out. println(“The value of x is:”+x);
d.
import java.util.*;
public class Main
{
public static void main(String[] args) {
int m=10;n=20;
String result=(m==n)?"equal::"not equal";
System.out.println(result);
}
}
Ans:
int m=10,n=20;
(m==n)?”equal”:”not equal”
24.Write the output for the below programs

a.public class program


{
public static void main(String argos[])
{
int i=10;
System.out.println(i%2);
}
}
Ans:_____0___

b. public class SA2R1


{
public static void main(String[] args)
{
float first = 1.20f, second = 2.45f;
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
float temporary = first;
first = second;
second = temporary;
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
}
Ans:
First number = 1.2
Second number = 2.45
First number = 2.45
Second number = 1.2

c.
import java.util.*;
public class Main
{
public static void main(String[] args) {
int m2=21;
String result=(m2%2==0)?"even":"Odd";
System.out.println(result);
}
}
Ans: Odd

d.
import java.util.*;
public class Main
{
public static void main(String[] args)
{
char operator = '+';
int num1 = 8;
int num2 = 7;
int result = (operator == '+') ? (num1 + num2) : (num1 - num2);
System.out.println(result);
}
}
Ans:15

e.
public class SA2R1
{
public static void main(String[] args)
{
float first = 1.20f, second = 2.45f;
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
float temporary = first;
first = second;
second = temporary;
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
}
ANS: --BEFORE--
First number = 1.2
Second number = 2.45
--AFTER--
First number = 2.45
Second number = 1.2

25.Debug the following code snippets and write the correct statements in the blanks provided.

a. public class SA2R2 {


public static void main(String[] args) {
int dividend = 25, divisor = 4;
int quotient = ___________________
int remainder = _________________
System.out.println("Quotient = " + quotient);
System.out.println("Remainder = " + remainder);
}
}

Ans: int quotient = dividend / divisor; AND int remainder = dividend % divisor;
The output is Quotient = 6 and Remainder = 1
b. Create a variable named carName and assign the value Volvo to it
________ ____= ________;
Ans: String carName=Volvo;
c. Create a variable called z, assign x + y to it, and display the result.

int x = 5;
int y = 10;
__ ____ = x + y;
System.out.println(___);

Ans: int x = 5;

int y = 10;
int sum = x + y;
System.out.println(sum);

d. Use the correct operator to increase the value of the variable x by 1

int x = 10;

_____x;

Ans : ++x;

e. Use the addition assignment operator to add the value 5 to the variable x

int x = 10;

x_____ 5;

Ans: x+=5;

***************************************************************************

You might also like