0% found this document useful (0 votes)
7 views30 pages

Grade8 Term1 Wsak

The document is an answer key for a Grade 8 Computer Science Term 1 program focused on Java. It includes topics such as Java features, variables, data types, operators, and various programming exercises demonstrating the use of these concepts. The exercises cover basic printing, user input, arithmetic operations, and data handling using the Scanner class.

Uploaded by

gjkprimalia
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)
7 views30 pages

Grade8 Term1 Wsak

The document is an answer key for a Grade 8 Computer Science Term 1 program focused on Java. It includes topics such as Java features, variables, data types, operators, and various programming exercises demonstrating the use of these concepts. The exercises cover basic printing, user input, arithmetic operations, and data handling using the Scanner class.

Uploaded by

gjkprimalia
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

GRADE 8/COMPUTER SCIENCE/PROGRAMS ANSWER KEY /TERM-1

INTRODUCTION TO JAVA

INDEX
Sl No Topics

1 Introduction to JAVA
1.​ Features
2.​ Installation , Compilation and Execution
3.​ Character set
4.​ Tokens
5.​ Comments

2 Introduction to variables and datatypes


[Link] a Message using S.O.P
[Link] Types (int,string,char,float and double,bool) and Variables
3. Use of \n and \t
[Link] input -Scanner method(Data types-int,float,char,string
,double )

3 Operators in JAVA
[Link] Operator (+,-,*,/,%)
[Link] Operator (+=,-=,*=,/=,%=)
[Link] Operator / Ternary operator
[Link]/decrement operator
[Link] Operator(==,>,<,>=,<=,!=)

BASIC PRINT PROGRAM


1.​ Write a JAVA program to print To display the message “Welcome to Java
Programming!! as show below”
Output:

Code:
public class Message1
{
public static void main(String args[ ])
{
[Link]("Welcome to Java Programming!!” );
}
}
TO PRINT A STATEMENT IN A SPECIFIED FORMAT
2.​ Write a program to store name and age and display in the below Output:

Code:
public class program1
{
public static void main(String args[])
{
// Declaring variables to store value.
int age=15;
String name="Name";
char Section='A';
[Link]("My Name is :"+name);
[Link]("My age is :"+age);
}
}

PROGRAMS ON DATA TYPES,


3.​ 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="[Link] 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;
[Link] ( " \t BUS TICKET ");
[Link]("\n=========================================");
[Link]("Name of the Bus:\t” +nameofthebus);
[Link]("Train No:\t \t \t” +busno);
[Link]("\n---------------------------------------");
[Link]("From\t \t To ");​
[Link](""+from+"\t \t \t "+to);​ ​ ​
[Link]("Seat\t \t \t "+seat);​ ​
[Link]("Date\t \t \t Time(in pm) "); ​​ ​
[Link](""+date+"\t \t "+boardingtime);
[Link]("Price\t \t \t "+price); ​ ​ ​
[Link]("\n---------------------------------------");
[Link]("PASSENGER DETAILS: ");​​
[Link]("\n---------------------------------------");
[Link]("Name:" +nameofpassenger);
[Link]("Age:"+age);​
}
}

4.​ 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="[Link]";
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;
[Link] ( " \t TRAIN TICKET\t ");
[Link]("\n=========================================");
[Link]("Name of the Train:\t \t” +nameofthetrain);
[Link]("Coach:\t \t \t \t “+coach);
[Link]("Train No:\t \t \t \t” +trainno);
[Link]("\n---------------------------------------");
[Link]("From\t \t To ");​
[Link](""+from+"\t \t "+to);​ ​ ​
[Link]("Class\t \t \t Seat ");​ ​
[Link](""+clas+"\t \t \t "+seat);​ ​
[Link]("Date\t \t \t Time(in pm) ");​ ​ ​
[Link](""+date+"\t \t "+boardingtime);​​ ​
[Link]("\n---------------------------------------");
[Link]("PASSENGER DETAILS: ");​​
[Link]("\n---------------------------------------");
[Link]("Name:" +nameofpassenger);
[Link]("Age:"+age);​
}
}

5.​ 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;
[Link] ( "\t BOARDING PASS ");
[Link]("\n=========================================");
[Link]("Name of the Flight:\t\t\t”+nameoftheflight);
[Link]("Carrier:\t\t\t\t”+carrier);
[Link]("FLIGHT No:\t\t\t\t”+flightno);
[Link]("\n---------------------------------------");
[Link]("From\t\t\tTo ");​
[Link](""+from+"\t\t"+to);​
[Link]("Class\t\t\tSeat ");​
[Link](""+clas+"\t\t\t"+seat); ​ ​
[Link]("Date\t\t\tTime ");​ ​ ​
[Link](""+date+"\t\t"+boardingtime);​ ​ ​
[Link]("\n---------------------------------------");
[Link]("PASSENGER DETAILS: ");​​
[Link]("\n---------------------------------------");
[Link]("Name:" +nameofpassenger);
[Link]("Age:"+age);​

}
}

PROGRAMS IN JAVA WITHOUT USING SCANNER METHOD


ARITHMETIC OPERATOR

6.​ Write program to find the diameter, volume and surface area of the sphere [Formula:
Surface Area, A = 4πr2 ; Volume, V = ⁴⁄₃πr3 ; Diameter = 2r, Where,r= radius of a
sphere]

OUTPUT
Code:
import [Link].*;
public class SA2 ​ ​ ​ ​ ​ ​ ​ ​
{ ​ ​ ​
public static void main(String[] args)
{
​ int r;
​ Scanner SC=new Scanner([Link]);​
​ [Link]("Please enter the radius of the sphere (in cm) ");
​ r=[Link]();


​ double SA= 4*(3.14*r*r);
​ double V= 1.33*(3.14*r*r*r);
​ float D=2*r;
​ [Link] ("The Diameter of the Sphere is " +D+ " cm");
[Link] ("The Surface Area of the Sphere is " +SA+ " cm^2");
​ [Link] ("The Volume of the Sphere is " +V+ " cm^3");

}
}

7.​ Write a program to calculate the area of a rectangle with width= 8,length=6 as shown
in the screenshot below.
OUTPUT
Code:
public class rectangle
{
public static void main(String[] args)
{
int w=8,l=6;
float area=w*l;
[Link]("Area of Rectangle is :"+area);
}
}
ASSIGNMENT OPERATOR WITH BOOLEAN DATATYPE

8.​ Simple program illustrating boolean datatype.


Output:

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

PROGRAMS USING SCANNER CLASS


DATATYPES

9.​ Write a program to accept the text as “Introduction to Java Programming Language”
and display it as shown in the below format.
OUTPUT
Code:
import [Link].*;
​ public class ScannerExample {
​ public static void main(String args[]){
​ Scanner obj = new Scanner([Link]);
[Link]("Enter the text to be displayed: ");
​ String name = [Link]();
[Link]("text is: " + name);

}
}

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

OUTPUT

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

11.​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 [Link].*;
public class studentinformation {
public static void main(String args[])
{

​ ​ Scanner SC=new Scanner([Link]);
​ ​ [Link]("Enter the name");
​ ​ String name=[Link]();
​ ​ [Link]("Enter the age");
​ ​ int age=[Link]();
​ ​ [Link]("Enter the your interest");
​ ​ String interest=[Link]();
​ ​ [Link]("Enter the grade");
​ ​ int grade=[Link]();
​ ​ [Link]("Enter the sec");
​ ​ char sec=[Link]().charAt(0);
[Link]("\n \n Hey ,My name is" + name +" and I am "+age+" years old.I study
in "+grade+sec+" My area of interest is:"+interest);
}
}
ARITHMETIC OPERATOR WITH SCANNER
12.​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 [Link].*;
public class Main
{
​ public static void main(String[] args)
{
​ ​ Scanner in=new Scanner([Link]);
​ ​ [Link]("Type a number");
​ ​ int num1=[Link]();
​ ​ [Link]("Type another number");
​ ​ int num2=[Link]();
​ ​ [Link]("Sum is:"+(num1+num2));
​ ​ [Link]("Difference is:"+(num1-num2));
​ ​ [Link]("Product is:"+(num1*num2));
​ ​ [Link]("Quotient is:"+(num1/num2));
}
}

13.​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 [Link].*;
public class Main
{
public static void main(String args[])
{
double HRA,CA,basic,gross;
Scanner sc=new Scanner([Link]);
[Link]("Enter Basic salary ");
basic=[Link]();
[Link]("Enter HRA ");
HRA=[Link]();
[Link]("Enter CA ");
CA=[Link]();
gross=basic+HRA+CA;
[Link]("Gross Salary= Rs. "+gross);
}
}

14.​ 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 [Link].*;
public class SI
{
public static void main(String args[])
{
​ ​ int p,t;
​ ​ float r,si;
​ ​ Scanner SC=new Scanner([Link]);
​ ​ [Link]("Enter the Principle Amount (in rupees)");
​ ​ p=[Link]();
​ ​ [Link]("Enter the Rate Of Interest (in percentage)");
​ ​ r=[Link]();
​ ​ [Link]("Enter the Time Period (in years)");
​ ​ t=[Link]();
​ ​ si=(p*t*r)/100;
​ ​ [Link]("\n \n The Simple Interest is :" +si+ "Rupees");
}
}

15.​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 [Link].*;
public class temperature {
public static void main(String args[])
{

​ ​ double f,c;
​ ​ Scanner SC=new Scanner([Link]);
​ ​ [Link]("Enter the fahrenheit ");
​ ​ f=[Link]();
​ ​ c=(f-32)*(0.55);​
​ ​ [Link]("The temperature in Fahrenheit is " +f+ " degree. ");​
​ ​ [Link]("After converting Fahrenheit to Celsius, ");​ ​
​ ​ [Link]("the temperature is " +c+ " degree Celsius ");
}
}
16.​ Program to accept length, width,and height of a Rectangular prism and find its
volume in cubic cms and Surface area in [Link] and display the output as shown in the
below format.
Formula are: Volume=lwh; Surface area=2(wl+hl+hw)

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

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

OUTPUT

Code:
import [Link].*;
public class Main
{
​ public static void main(String[] args) {
​ Scanner in=new Scanner([Link]);
​ [Link]("Enter any number");
​ int x=[Link]();
​ [Link]("Square of a number is= "+(x*x));
​ [Link]("Cube of a number is = "+(x*x*x));
}
}
INCREMENT AND DECREMENT OPERATOR

18.​Write a program to assign the value of a as 10 and b as [Link] 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;
[Link]("Value of a: " + a);
result1 = ++a;
[Link]("After pre-increment: " + result1);
[Link]("Value of b: " + b);
result2 = b++;
[Link]("After post-increment: " + result2);
}
}
19.​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;
[Link]("Value of c: " + c);
result1 = --c;
[Link]("After pre-decrement: " + result1);
[Link]("Value of d: " + d);
result2 = d--;
[Link]("After post-decrement: " + result2);

}
}
TERNARY /CONDITIONAL OPERATOR
20.​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";
[Link](result);
}
}
21.​Write a program to find if the number entered by the user is a factor of 5 or not( using
Ternary Operator) as shown in the screenshot below.
OUTPUT​ ​
Code:

import [Link]; ​
public class factor
{ ​
​ public static void main(String[] args)
{ ​
​ Scanner sc = new Scanner([Link]);
​ [Link]("Enter a number: "); ​
​ int number = [Link](); ​
​ String c = ((number % 5 )==0)? "5 is a Factor of " : "5 is not a Factor of";
[Link](c+ " " +number);

}
​ }

22.​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 [Link];​ ​ ​ ​
public class rttriangle
{​ ​ ​ ​ ​
public static void main(String[] args)
{​ ​
Scanner sc = new Scanner([Link]);​ ​
[Link]("Enter the base(in cm): ");​​
int s1 = [Link]();
[Link]("Enter the perpendicular(in cm): ");​
int s2 = [Link]();​ ​ ​ ​ ​
[Link](" Enter the Hypotenuse(in cm): ");​
int s3 = [Link]();
String c = ((s1*s1)+(s2*s2)==(s3*s3))? " a Right Angled Triangle" : "Not a Right
Angled
Triangle";​
​ [Link]("The triangle is " +c+ " ,where base= " +s1+"cm
Perpendicular= " +s2+"cm
and Hypotenuse = " +s3+"cm");​ ​ ​
​ }
​ }
Conditional & Logical Operators
1.​ To calculate the age and check eligibility to take admission in grade8 by declaring the
current year as constant using appropriate operators as shown in the screenshot below.

Age criteria is between 13 to 14 years.


Output1:

Output2:

Code:
import [Link];
public class Eligibility
{
//declaring constant
private static final int Current_Year=2024;
public static void main(String[] args)
{
Scanner in=new Scanner([Link]);
[Link]("Enter year of birth");
int Birth_year=[Link]();
int age=Current_Year-Birth_year;

String result=(age>=13&&age<=14)?"Eligible":"not eligible";


[Link]("You are "+result+" to take admission for grade8");
}
}
APPLICATION BASED QUESTIONS

II..List the errors in the given code


1.​ public class program10
public static void main (String args[])
{
int number1='4';
String 4='test';
}
Ans: int number1=4;
String ABS="test";
A digit cannot be a variable name
2.​ public class program11
{
public static void main (String args[])
{
int _number2=10;
}
}
Ans:No Error
3.​ public class program12
{
public static void main (String args[])
{
class String a="Name";
}
}
Ans:String a="Name"; should not use class
4.​ public class program13
public static void main (String args[])
{
"Name" = x String;
}
Ans:String x="Name";
5.​ public class program14
public static void main (String args[])
{
int @number1=600;
}
Ans:int number1=600;
Variable name cannot start with@
6.​ public class program15 {
public static main(String[] args) {
[Link]("Hello World Example);
}
}
Ans :[Link]("Hello World Example”);

7.​ public class program16 {

public static void main (String args[])


{
String super="test";
}
Ans:String Super="test"; [super is a keyword]

III. Do the below programs have any errors?If yes,mention and write the output?

1.​ public class programa {

public static void main (String args[])


{
int _a = 10;
int $b = 20;
int C = 30;
int c = 40;
int result = _a + $b + C + c;
[Link]("Result: " + result);
}
Ans:No errors,Result = 100
2.​ public class programb
{
public static void main(String[] args)
{
[Link]("My name is:");[Link]("Jane Doe");[Link]("I
am studying in Gr8"); }
}
Ans:My name is:
Jane Doe
I am studying in Gr8

3.​ public class programc


{
​ int x = 10;
​ public static void main(String[] args)
​ {
​ [Link]("The number is" +x);
}
}
Ans:public class programc
public static void main(String[] args)
​ {
​ ​ int x = 10;
​ ​ [Link]("The number is" +x);
}
}

[Link] 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;
​ ​ [Link]( "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;
[Link] ("a+b"+""+"a-b");
[Link] (sum+""+subtr);
}
}

Ans:sum

c. import [Link] *;
public class Main
{
​ public static void main(String[] args) {
​ ​ Scanner in=new Scanner([Link]);
​ ​ [Link]("Enter the integer value of x");
​ ​ int x=[Link]();
​ ​ [Link]("The value of x is:"+X);
​ }
}
Ans.
import [Link].*;
int x=[Link]();
[Link]. println(“The value of x is:”+x);
d.
import [Link].*;
public class Main
{
​ public static void main(String[] args) {
​ ​ int m=10;n=20;
​ ​ String result=(m==n)?"equal::"not equal";
​ ​ [Link](result);
​ }
}
Ans:
int m=10,n=20;
(m==n)?”equal”:”not equal”

e. public class SA2R2 {


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

Ans: int quotient = dividend / divisor; AND int remainder = dividend % divisor;
The output is Quotient = 6 and Remainder = 1

f.

Ans: Output : error: cannot find symbol,


[Link]("The value of x - y is: "+subtaction);
g.
Output : error: variable num might not have been initialized
num=0; (num can be any number)
To rewrite the code
f. Create a variable named carName and assign the value Volvo to it
________ ____= ________;
Ans:​ String carName=Volvo;
g. Create a variable called z, assign x + y to it, and display the result.

int x = 5;
int y = 10;
__ ____ = x + y;
[Link](___);

Ans: ​ int x = 5;

int y = 10;
int sum = x + y;
[Link](sum);

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

int x = 10;

_____x;

Ans : ++x;

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

int x = 10;

x_____ 5;

Ans: x+=5;
V .Write the output for the below programs

[Link] class program


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

b. public class SA2R1


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

c.
import [Link].*;
public class Main
{

You might also like