Lab Manual
Lab Manual
Lab Manual
Program:
import java.util.*;
class Welcome{
String str=obj.nextLine();
System.out.print("Welcome "+str);
Output:
1 b) Write a java program that prompts the user for an integer and then
prints out all the prime numbers up to that integer.
Program:
import java.util.Scanner;
class PrimeNumber
for(int i=2;i<=n;i++)
{
count=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
count=1;
}
if(count==0){
System.out.print(" "+i);
Output:
Program:
import java.util.Scanner;
class Rectangle
{
double length,width;
void read_attributes()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Rectangle:");
length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
width = scanner.nextDouble();
void area()
{
double area=length*width;
System.out.println("Area of Rectangle ="+area);
}
void perimeter()
{
double perimeter=2*(length+width);
System.out.println("perimeter of Rectangle ="+perimeter);
class CalRectangle{
public static void main (String[] args)
{
Rectangle rec=new Rectangle();
rec.read_attributes();
rec.area();
rec.perimeter();
}
}
Output:
Program:
import java.util.*;
class Fibo
{
public static void main(String args[])
{
int c=1,n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
n=sc.nextInt();
int a=1;
int b=0;
}
}
}
Output:
Non-Recursive:
Program:
import java.util.Scanner;
class Factorial_non
{
public static void main(String[] args)
{
int n, mul = 1;
Scanner s = new Scanner(System.in);
System.out.print("Enter any integer:");
n = s.nextInt();
for(int i = 1; i <= n; i++)
{
mul = mul * i;
}
System.out.println("Factorial of "+n+" :"+mul);
}
}
Output::
Recursive:
Program:
import java.util.Scanner;
class Factorial{
public static void main(String args[]){
Program:
import java.util.*;
public class Palindromes
{
public static void main(String args[])
{
String str1, str2 = "";
Scanner s = new Scanner(System.in);
System.out.print("Enter the string:");
str1 = s.nextLine();
int n = str1.length();
for(int i = n - 1; i >= 0; i--)
{
str2 = str2 + str1.charAt(i);
}
if(str1.equalsIgnoreCase(str2))
{
System.out.println("The string is palindrome.");
}
else
{
System.out.println("The string is not palindrome.");
}
}
}
Output:
Method Overloading
Program:
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
void test(int a) {
System.out.println("a: " + a);
}
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
Output:
Method overriding
Program:
class Vehicle{
void run(){
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle{
void run()
{
System.out.println("Bike is running safely");
}
}
class Override{
public static void main(String args[])
{
Output:
Program:
class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
class B extends A {
void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A {
void callme() {
System.out.println("Inside C's callme method");
}
}
class Override{
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
A r;
r = a;
r.callme();
r = b;
r.callme();
r = c;
r.callme();
}
}
Output:
Program:
class Superclass{
int a ,b;
Superclass() {
a=10;
b=20;
}
}
class Subclass extends Superclass{
int c;
void display() {
System.out.println("values in subclass:a="+a+" b="+b);
}
void add()
{
c=a+b;
System.out.println("addition of values in subclass:"+c);
}
class Demo_inherit{
public static void main(String args[]) {
import java.util.Scanner;
void square(double s)
{
double area = s*s;
System.out.println("Area of Square: "+area);
}
void circle(double r)
{
double area = 3.14*r*r;
System.out.println("Area of Circle: "+area);
}
}
class Calcarea {
public static void main(String args[])
{
double l, b, h, r, s;
Shape area = new Shape();
Scanner get = new Scanner(System.in);
GRectangle,java
package Geometry;
GSquare.java
package Geometry;
Demo_packages.java
import Geometry.*;
class Demo_packages
{
public static void main(String args[])
{
}
}
Output:
Predefined Exception
Program
class Demo_Exception {
public static void main(String args [ ])
{
int d, a;
try
{
d = 0;
a = 42 / d;
System.out.println("This will not be printed.");
}
catch (ArithmeticException e)
{
System.out.println("Division by zero.");
}
System.out.println("After catch statement.");
}
}
Output:
Division by zero.
After catch statement.
class Demo_Exception {
static void demoproc() {
try {
throw new NullPointerException("demo");
}
catch(NullPointerException e)
{
System.out.println("Caught inside demoproc.");
throw e; // rethrow the exception
}
}
public static void main(String args[]) {
try {
demoproc();
}
catch(NullPointerException e)
{
System.out.println("Recaught: " + e);
}
}
}
Output:
Thread class
Program:
firstThread.start();
secondThread.start();
}
}
Output:
Program:
t1.start();
t2.start();
}
}
Output:
Program:
import java.awt.*;
import java.applet.*;
/*
<applet code="Demo_Applet" width="400" height="200">
<param name="Name" value="AITAM">
<param name="Location" value="Tekkali">
</applet>
*/
Program:
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
System.out.print(Thread.currentThread().getName());
System.out.println("Main thread priority : "+ Thread.currentThread().getPriority());
Thread.currentThread().setPriority(10);
System.out.println("Main thread priority : "+ Thread.currentThread().getPriority());
}
}
Output:
t1 thread priority : 5
t2 thread priority : 5
t3 thread priority : 5
t1 thread priority : 2
t2 thread priority : 5
t3 thread priority : 8
mainMain thread priority : 5
Main thread priority : 10