Java Assmt Solution 1
Java Assmt Solution 1
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);
}
}
char c=s1.next().charAt(0);
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");
}//main
}//class
}//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 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”.
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”.
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”);