Java Assignment
Java Assignment
CODE –
class Sub {
void sum() {
int a, b, c;
a = 10;
b = 20;
c = a + b;
System.out.println("\n*** Concept of Encapsulation ***\n");
System.out.println("a = " + a + ", b = " + b);
System.out.println("Sum of a and b : " + c);
}
}
public class Assignment1 {
public static void main(String[] args) {
Sub object = new Sub();
object.sum();
}
}
OUTPUT -
JAVA Assignment
Q.2 Write a program that use Boolean data type and print the
Prime number series up to 50.
CODE -
public class Assignment2 {
public static void main(String[] args) {
boolean b = true;
System.out.println("*** Prime Number series upto 50 ***\n");
System.out.println("Prime numbers - ");
for(int i =1; i<=50;i++)
{
b = true;
for(int j =2;j<=i/2;j++){
if(i%j==0)
{
b = false;
}
}
if(b)
System.out.print( i + " ,");
}
}
}
OUTPUT -
JAVA Assignment
CODE –
import java.util.Scanner;
import java.lang.Math;
public class Assignment3 {
static boolean isArmstrong(int n) {
int temp, digits = 0, last = 0, sum = 0;
temp = n;
while (temp > 0) {
temp = temp / 10;
digits++;
}
temp = n;
while (temp > 0) {
last = temp % 10;
sum += (Math.pow(last, digits));
temp = temp / 10;
}
if (n == sum)
return true;
else
return false;
}
public static void main(String args[]) {
int num;
Scanner sc = new Scanner(System.in);
JAVA Assignment
num = sc.nextInt();
if (isArmstrong(num)) {
System.out.print("Armstrong number");
} else {
System.out.print("Not Armstrong number");
}
sc.close();
}
}
OUTPUT –
JAVA Assignment
CODE -
public class Assignment4 {
public static void main(String[] args) {
int arr[] = { 7, 8, 3, 1, 2 };
System.out.println("Before the sorting : ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("\nAfter sorting :");
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
JAVA Assignment
OUTPUT –
JAVA Assignment
Q.5 Write a Java method to find the smallest number among three
numbers.
CODE –
CODE –
import java.util.Scanner;
public class Assignment6 {
static int wordcount(String string) {
int count = 0;
char ch[] = new char[string.length()];
for (int i = 0; i < string.length(); i++) {
ch[i] = string.charAt(i);
if (((i > 0) && (ch[i] != ' ') && (ch[i - 1] == ' ')) || ((ch[0] != ' ') && (i == 0)))
count++;
}
return count;
}
public static void main(String[] args) {
System.out.println("*** count all words in a string ***\n");
System.out.println("Ente few words : -");
Scanner sc = new Scanner(System.in);
String string = sc.nextLine();
System.out.println(wordcount(string) + " words.");
sc.close();
}
}
OUTPUT –
JAVA Assignment
CODE –
CODE -
import java.util.Scanner;
CODE -
import java.util.Scanner;
OUTPUT -
JAVA Assignment
CODE -
CODE -
CODE –
import java.util.Scanner;
public class Assignment12 {
public static void main(String args[]) {
float principalAmount, rate, time, simpleIntrest;
Float p, r, t, SI;
Scanner sc = new Scanner(System.in);
System.out.println("*** Simple Intrest using Wrapper class ***\n");
System.out.print("Enter the principal Amount : - ");
principalAmount = sc.nextFloat();
System.out.print("Enter the Rate : - ");
rate = sc.nextFloat();
System.out.print("Enter the Time (in years) : - ");
time = sc.nextFloat();
p = principalAmount;
r = rate;
t = time;
simpleIntrest = (p * r * t) / 100;
SI = simpleIntrest;
System.out.println("Simple Interest is: " + SI);
sc.close();
}
}
JAVA Assignment
OUTPUT -
JAVA Assignment
CODE –
CODE –
interface Area{
final static float pi = 3.14f;
float compute(float x , float y);
}
interface RectArea{
int calculate(int i,int w);
}
class Test implements Area, RectArea{
public float compute(float x , float y){
System.out.println("\npi = " +pi+" x = " +x+" y = " + y);
return(pi*x*y);
}
public int calculate(int l, int w){
System.out.println("\nlength = " +l+" width = " +w);
return(l*w);
}
}
public class Assignment14 {
public static void main(String[] args) {
Area A;
RectArea R;
Test T = new Test();
A = T;
JAVA Assignment
OUTPUT –
JAVA Assignment
CODE –
OUTPUT –
JAVA Assignment
CODE –
class Vehicle {
void run() {
System.out.println("In childwood TMKOC was my fav TV serial. s");
}
}
class Bike extends Vehicle {
void run() {
System.out.println("Now OTT platform web series is my fav .");
}
public static void main(String args[]) {
System.out.println("*** Method Overriding Program ***\n");
Bike obj = new Bike();
obj.run();
}
}
CODE -
JAVA Assignment
CODE –
}
}
OUTPUT -
JAVA Assignment
CODE -
class Dad{
String rs = "5 lacks";
void superMethod(){
System.out.println(" (parent class) Dad's properties " + rs);}
}
class Me extends Dad{
String myRs = "2 lacks";
void childMethod(){
System.out.println("My properties " + myRs);
System.out.println("(child class) I can access my Dad's all properties.");}
}
class Assignment18{
public static void main(String args[]){
System.out.println("*** Single Inhertance Example ***\n");
Me obj=new Me();
obj.childMethod();
obj.superMethod();
}}
OUTPUT -
JAVA Assignment
CODE –
class Vechicle {
void VechicleTypes() {
System.out.println("There are many types of Vechicle : - \n Cars , bikes , Scooty ,
Trucks , buses");
}
}
class Car extends Vechicle {
String price = "15 lacks to 40 lacks";
void CarsTypes() {
System.out.println("");
System.out.println(" There are many famous cars compaines : ");
System.out.println("Maruti , Tata , XUV , Range Rover , Invova , Toyota.");
}
}
class Invova extends Car {
String myPrice = "15 lacks to 25lacks";
void InvovaCars() {
System.out.println("");
System.out.println("My fav car is Invova and I can Buy it.");
System.out.println("Invova Cars price " + price);
System.out.println("My Bugut " + myPrice);
}
}
OUTPUT –
JAVA Assignment
CODE -
class ParentA {
public void displayA() {
System.out.println("This is ParentA class");
}
}
class ParentB {
public void displayB() {
System.out.println("This is ParentB class");
}
}
class Child extends ParentA {
public void displayA() {
System.out.println("This is Child class A part");
}
}
class GrandChild extends Child {
ParentB b = new ParentB();
public void displayB() {
b.displayB();
System.out.println("This is GrandChild class B part");
}
}
public class Assignment20b {
public static void main(String[] args) {
System.out.println("\n *** Hybrid Inheritance *** \n");
JAVA Assignment
OUTPUT -
JAVA Assignment
CODE –
class Base {
public void display() {
System.out.println("I am Parent class");
}
}
class Derived1 extends Base {
public void display2() {
System.out.println("This is Child1 class");
}
}
class Derived2 extends Base {
public void display3() {
System.out.println("This is Child2 class");
}
}
public class Assignment21 {
public static void main(String[] args) {
System.out.println("\n *** Hierarchical Inheritance *** \n");
Derived1 d1 = new Derived1();
d1.display();
d1.display2();
System.out.println("\n");
Derived2 d2 = new Derived2();
d2.display();
d2.display3();
JAVA Assignment
}
}
OUTPUT -
JAVA Assignment
CODE –
interface Bank {
public void user();
public void password();
}
public class Assignment22 implements Bank {
public void user() {
System.out.println("your name - : ishwar nishad");
}
public void password() {
System.out.println("your password - : Ishu@123 ....");
}
public static void main(String[] args) {
System.out.println("\n*** Interface Program ***\n");
System.out.println("Show my Bank Details : - \n");
Assignment22 obj = new Assignment22();
obj.user();
obj.password();
}
}
JAVA Assignment
OUTPUT -
JAVA Assignment
CODE –
interface Printable {
void print();
}
interface Showable {
void Show();
}
public class Assignment23 implements Printable, Showable {
public void print() {
System.out.println("called Method of Interface 1");
}
public void Show() {
System.out.println("called Method of Interface 2");
}
public static void main(String[] args) {
Assignment23 obj = new Assignment23();
System.out.println("\n*** Multiple Interface Example ***\n");
// System.out.println("multiple interface example");
obj.print();
obj.Show();
System.out.println();
}
}
JAVA Assignment
OUTPUT –
JAVA Assignment
CODE –
interface Shape {
double getArea();
}
interface Colorable extends Shape {
void fillColor();
}
class Square implements Colorable {
double side;
String color;
Square(double s, String c) {
side = s;
color = c;
}
public double getArea() {
return side * side;
}
public void fillColor() {
System.out.println("Filling color " + color + " in square");
}
}
OUTPUT -
JAVA Assignment
CODE –
OUTPUT -
JAVA Assignment
CODE –
OUTPUT -
JAVA Assignment
CODE –
Thread.sleep(5000);
t.resume();
System.out.println("Thread resumed");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
OUTPUT –
JAVA Assignment
Q.28 Write a program and use the this and super keyword.
CODE –
class Parent {
int value;
Parent(int v) {
value = v;
}
}
class Child extends Parent {
int value;
Child(int v1, int v2) {
super(v1);
value = v2;
}
public void display() {
System.out.println(" Parent Age: " + super.value);
System.out.println("Child Age: " + this.value);
}
}
public class practical28 {
public static void main(String[] args) {
System.out.println("\n *** super and this keyword *** \n");
Child c = new Child(50, 30);
c.display();
}
}
JAVA Assignment
OUTPUT -
JAVA Assignment
CODE :-
Creating Package
//Package Name
package practical29p;
public class Demo {
public void show() {
System.out.println("Hi Everyone");
}
public void view() {
System.out.println("Hello");
}
}
Import Package
import practical29p.Demo;
public class Practical29b {
public static void main(String arg[]) {
Demo d = new Demo();
d.show();
d.view();
}
}
JAVA Assignment
Output :-
JAVA Assignment
Q.30 Write a program to read data from using java i/o package
(using character class).
CODE –
import java.io.*;
}
JAVA Assignment
}
}
OUTPUT –
JAVA Assignment
CODE –
import java.io.FileWriter;
import java.io.IOException;
obj.write("Assignment no . 31");
obj.write(" by - ishwar nishad");
System.out.println("file created successfully ...");
obj.close();
}
}
OUTPUT -
JAVA Assignment
Q.32 Write a program to read data from using java i/o package
(using byte class).
CODE –
import java.io.FileInputStream;
public class Assignment32 {
public static void main(String args[]){
System.out.println("\n *** Read data using Byte Class *** \n");
System.out.println("data : - ");
try{
FileInputStream fin=new FileInputStream("ishwar.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.print((char)i);
}
fin.close();
}catch(Exception e){System.out.println(e);}
}
}
OUTPUT –
CODE –
import java.io.*;
class Assignment33 {
public static void main(String[] args)
throws IOException
{
int i;
FileOutputStream obj = new FileOutputStream("ishwar2.txt",true);
String st = "Ishwar is writing in me";
char ch[] = st.toCharArray();
for (i = 0; i<st.length(); i++) {
obj.write(ch[i]);
}
System.out.println("\n *** Write in file using Byte class *** \n");
System.out.println("written successfully ...");
obj.close();
}
}
JAVA Assignment
OUTPUT –
CODE –
import java.io.*;
import java.util.*;
}
catch (Exception e){
System.out.println(e);
JAVA Assignment
}
}
}
OUTPUT –
CODE -
JAVA Assignment
import java.sql.*;
import java.util.*;
Assignment36{
public static void main(String a[])
{
if (obj2 == 1)
System.out.println("successfully inserted : " + sql);
else
System.out.println("insertion operation is failed");
con.close();
}
catch (Exception ex) {
System.err.println(ex);
}
}
}
OUTPUT –