0% found this document useful (0 votes)
9 views

Java Assmt Solution 1

These are the solutions to the java assignment for class 12 IT code 802 uploaded earlier :D

Uploaded by

Geet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Java Assmt Solution 1

These are the solutions to the java assignment for class 12 IT code 802 uploaded earlier :D

Uploaded by

Geet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

SOLUTION

SECTION A
1 Program to swap / exchange values of two variables -
a) Using 3rd variable
public class Main {
public static void main(String[] args) {
int a=10, b=20;
int c=b;
b=a;
a=c;
System.out.println(a+" "+b);
}
}

b) Without using 3rd variable


public class Main {
public static void main(String[] args) {
int a=10, b=20;
a=a+b;
b=a-b;
a=a-b;
System.out.println(a+" "+b);
}
}

2 Program to check if an Input character is an alphabet or not . If an alphabet , further


check and print if its upper case alphabet or lower case alphabet
package ITXII;
import java.util.Scanner;
public class Q2
{
public static void main(String[] args) {

Scanner s1=new Scanner(System.in);


System.out.println("Ente char ");

char c=s1.next().charAt(0);

if ((c>='a' && c <='z') || (c>='A' && c <='Z'))


{
System.out.println("Alphabet ");
if (c>='a' && c <='z')
System.out.println("Lower case Alphabet
");
else
System.out.println("Upper case Alphabet
");
}
else
System.out.println("not an alphabet ");
}
}
3 Program to check and display if an Input character is alphanumeric character or a
special character
Scanner s1=new Scanner(System.in);
System.out.println("Enter char ");

char c1=s1.next().charAt(0);

char c=Character.toLowerCase(c1);
if ((c>='a' && c <='z') || (c>='0' && c <='9'))
System.out.println("Alphanumeric ");
else
System.out.println("Special character");

4 Program to generate the reverse of a string and check if it is a Palindrome or not.


import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner s1=new Scanner(System.in);

System.out.println("Enter string ");


String snew ="";
String s=s1.next();
int l=s.length();
for(int i=l-1;i>=0;--i)
snew=snew+s.charAt(i);
//error in snew=snew.concat(s[i]);
System.out.println("Reversed string "+snew);
if (s.equals(snew))
System.out.println("Palindrome ");
else
System.out.println("Not a palindrome");

}//main
}//class

5 Write program for a SIMPLE CALCULATOR using switch case


done in XI
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s1=new Scanner(System.in);
int n1,n2; float r;
System.out.println("Enter no1 no2 ");
n1=s1.nextInt();
n2=s1.nextInt();
System.out.println("Enter operation ");
char c1=s1.next().charAt(0);
switch(c1)
{
case '+': r=n1+n2;
System.out.println("Result = "+r);
break;
case '-': r=n1-n2;
System.out.println("Result = "+r);
break;
case '*': r=n1*n2;
System.out.println("Result = "+r);
break;
case '%': r=n1%n2;
System.out.println("Result = "+r);
break;
case '/': r=n1/n2;
System.out.println("Result = "+r);
break;
default: System.out.println("Invalid operator");
break;

}//switch
}//main
}//class

SECTION B - LOOPS
14
int t=1;
for (int i=1:i<=n;++i)
{
print(t);
t=t+i;
}

SECTION C - QUESTIONS
1 Why Java is considered an Object-Oriented Programming (OOP) language ?
JAVA is Object Oriented Programming Language as it supports the features of object oriented
languages like Data Encapsulation , Inheritance, Polymorphism,abstraction etc

Features of Java :
1. Object Oriented
2. Platform independent
3. Used for diverse applications
4. Robust language

A 2 Diverse computer applications that can be created using Java .


1. Database applications
2. Desktop applications,
3. Web based applications
4. Mobile applications
5. Games etc.

A 3 Java is case sensitive language because lower case and upper case words are treated
differently . All keywords , package names , built in class names should be used in the same case
as defined in Java language
e.g. INT x=0; will be incorrect . It should be int x=0;
WHILE (i <=10) is incorrect , while is the correct keyword to use.
Variable names written in capital letters differ from variable names with the same spelling but
written in small letters. For example, the variable name “percentage” differs from the variable
name “PERCENTAGE” or “Percentage”.

A 4 Java development kit (JDK)


A 5 a) The Scanner class is used to get user input, and it is found in the java.util package.
To use the Scanner class, create an object of the class and use any of the available methods found in
the Scanner class .
example: Scanner s= new Scanner(System.in );
above statement creates an object of Scanner class by name s and links it with standard input stream
System.in
String X = s.next(); reads the user input as a string and stores in variable X
b) Static method toString() of Integer class - Integer.toString(int )
converts given integer to string and returns string
c) Static method parseInt() of Integer class - Integer.parseInt(string )
converts given string to integer value and returns int
d) Static method parseFloat() of Integer class - Integer.parseFloat(string )
converts given string to float value and returns float
e) Static method parseDouble() of Integer class - Integer.parseDouble(string )
converts given string to double value and returns double
A 6 Definition of Compiler /Role of a Compiler - Compiler is a language translator software
which translates high level program code into machine language code that will be understood by
the computer.
Compiler also checks program for correct syntax and reports syntax errors .
After a program is compiled, the machine code can be executed on the computer, e.g. Windows,
for which it was compiled. If the program is to be executed on another platform, say Mac, the
program will first have to be compiled for that platform and can then be executed.

HOW JAVA COMPILER WORKS


A Java compiler instead of translating Java code to machine language code, translates it into Java
Bytecode .
Java Bytecode is a highly optimized set of instructions (also called a Java class file) .
When the bytecode is to be run on a computer, a Java interpreter, called the Java Virtual
Machine (JVM), translates the bytecode into machine code and then executes it.

The advantage of such an approach is that once a programmer has compiled a Java program
into bytecode, it can be run on any platform (say Windows, Linux, or Mac) as long as it has a
JVM running on it.
This makes Java programs platform independent and highly portable.

A 10 Program
A computer program is a sequence of instructions / programming statements given to the
computer in order to accomplish a specific task. The instructions tell the computer what to do
and how to do it.
Programs are written in a HLL (high level programming language) such as Java However, a
computer only understands “machine language”.

Comments , Single line , Multiline comments


Comments are used in code by programmers to document their programs - to provide
explanatory notes to other people who read the code. This is especially useful in the real world,
where large programs are written by one programmer and maintained by other programmers.
SINGLE LINE COMMENT
Beginning a comment line with two consecutive forward slashes (//)
MULTILINE COMMENTS
Writing the comment between the symbols /* and */
ADV Increase READABILITY OF CODE

SWITCH CASE
1 OUTPUT
Looking forward to the Weekend
Today is Saturday
2 Corrected code
int a=1;
switch(a)
{
default :
jTextField1.setText("transparent");
break;
case 0 : jTextField1.setText("Blue");
case 1 : jTextField1.setText("Red");
}
3 Rewrite the following program code using switch statement :
switch(color)
{
case 10: System.out.println("Red");
break;
case 20: System.out.println("Orange");
break;
case 30: System.out.println("Green");
break;
default:System.out.println ("Invalid");
break;
}
4 Code using Switch case
char c=’a’;
switch(c)
{
case ‘a’: System.out.println(‘Autumn’);
break;
case ‘w’: System.out.println(‘Winter’);
break;
case ‘r’: System.out.println(‘Rainy’);
break;
case ‘s’: System.out.println(‘Summer’);
break;

default:System.out.println ("Invalid");
break;
}
5 Code using if -else if- else
if (d==1 || d==2 || d==3 || d==5)
System.out.println(“WEEKDAY”);
else if (d==4 )
System.out.println(“HOLIDAY”);
else if (d==6 || d==7)
System.out.println(“WEEKEND”);
else System.out.println(“INVALID”);

You might also like