JAVA Program to calculate percentage marks of the student

/*
2. Write a program that calculate percentage marks of the student if marks of 6 subjects are given.
*/

import java.util.Scanner;

class Percent_pract2_2{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int sum = 0;
float avg;
int marks[] = new int[5];
System.out.println("Enter Marks of subject 1");
marks[0]=sc.nextInt();
System.out.println("Enter Marks of subject 2");
marks[1]=sc.nextInt();
System.out.println("Enter Marks of subject 3");
marks[2]=sc.nextInt();
System.out.println("Enter Marks of subject 4");
marks[3]=sc.nextInt();
System.out.println("Enter Marks of subject 5");
marks[4]=sc.nextInt();


for(int i=0;i<marks.length;i++){
sum += marks[i];
}
avg = (float)sum/marks.length;
System.out.println("Sum = "+sum);
System.out.println("percentage = "+avg);
}

}


JAVA program to convert rupees to dollar

/*
1. Write a JAVA program to convert rupees to dollar. 60 rupees=1 dollar
*/

class Convert_pract1_1
{
public static void main(String args[])
       {
int rs =Integer.parseInt(args[0]);
float dlr;
dlr= (float)rs/60;
System.out.println("Rs = "+rs);
System.out.println("Dollar = "+dlr);
}
}

File Handling using Object Input Output Stream

import java.io.*;
import java.util.*;
class ObjectReadWrite implements Serializable, Comparable <ObjectReadWrite>
{
int no;
String name;
ObjectReadWrite(int i,String s)
{
this.no=i;
this.name=s;
}
public String toString()
{
return "TP Data : "+this.no+":"+this.name;
}
public int compareTo(ObjectReadWrite a1)
{
return this.name.compareTo(a1.name);
}
public static void main(String args[]) throws Exception
{
TreeSet<ObjectReadWrite> aw = new TreeSet<ObjectReadWrite>();
aw.add(new ObjectReadWrite(1,"ABC"));
aw.add(new ObjectReadWrite(2,"DDF"));
aw.add(new ObjectReadWrite(3,"DAF"));
aw.add(new ObjectReadWrite(4,"DCF"));

//Writing Objects From TreeSet
ObjectOutputStream os=new ObjectOutputStream(
new FileOutputStream(
new File("Test.dat")));
os.writeObject(aw);
os.close();



//Reading Objects into TreeSet
TreeSet ar = new TreeSet();
ObjectInputStream is=new ObjectInputStream(
new FileInputStream(
new File("Test.dat")));

ar=(TreeSet)is.readObject();
is.close();


Iterator ir = ar.iterator();
while(ir.hasNext())
{
System.out.println((ObjectReadWrite)ir.next());
}

}
}

Use of HashMap

import java.util.*;
class BookId implements Comparator{
public int compare(Object o1,Object o2){
return ((Book)o1).book_id-((Book)o2).book_id;
}
}
class BookName implements Comparator{
public int compare(Object o1,Object o2){
return ((Book)o1).title.compareTo(((Book)o2).title);
}
}
public class Book implements Comparable{
/**
Unique Book ID
                */
public int book_id;
protected String title;
/**
Book Price including GST
                */
private float price;
/**
Constructor
                */
public Book(int id, String title, float price){
this.book_id = id;
this.title = title;
this.price = price;
}
public int compareTo(Object o1){
return (int)(this.price-(((Book)o1).price));
}
public String toString(){
return this.book_id+"#"+this.title+"#"+this.price+"@";
}

public static String ObjToString(Book c[]){
String str = new String();
for(int i=0;i<c.length;i++){
str = str+c[i];
}
return str;
}

public static Book[] StringToObj(String str){
String[] st = str.split("@");
Book b2[] = new Book[st.length];

for(int i=0; i<st.length;i++){
String st1[] = st[i].split("#");
b2[i] = new Book(Integer.parseInt(st1[0]),st1

[1],Float.parseFloat(st1[2]));
}
return b2;
}

public static void main(String args[]){

//ArrayList a1 = new ArrayList();
//TreeSet a1 = new TreeSet();
HashMap<Integer,Book> a1 = new HashMap<Integer,Book>();

a1.put(new Integer(1),new Book(2,"ADA", 200));
a1.put(new Integer(3),new Book(1,"Java", 500));
a1.put(new Integer(2),new Book(4,"MI", 800));
//Collections.sort(a1,new BookName());
Collection<Book> a2 = a1.values();
Iterator ir = a2.iterator();

while(ir.hasNext())
{
System.out.println((Book)(ir.next()));
}


/*System.out.print(Book.ObjToString(b));
String str1 = ObjToString(b);

Book c[] = StringToObj(str1);
for(int i=0;i<c.length;i++){
System.out.print(c[i]);
}*/
}
}

Use of Collection Framework

Use of Collection Framework Classes and Interfaces

ArrayList, TreeSet, Iterator

import java.util.*;
public class Book implements Comparable{
/**
Unique Book ID
                */
public int book_id;
protected String title;
/**
Book Price including GST
                */
private float price;
/**
Constructor
                */
public Book(int id, String title, float price){
this.book_id = id;
this.title = title;
this.price = price;
}
public int compareTo(Object o1){
return this.book_id-(((Book)o1).book_id);
}
public String toString(){
return this.book_id+"#"+this.title+"#"+this.price+"@";
}

public static String ObjToString(Book c[]){
String str = new String();
for(int i=0;i<c.length;i++){
str = str+c[i];
}
return str;
}

public static Book[] StringToObj(String str){
String[] st = str.split("@");
Book b2[] = new Book[st.length];
for(int i=0; i<st.length;i++){
String st1[] = st[i].split("#");
b2[i] = new Book(Integer.parseInt(st1[0]),st1[1],Float.parseFloat(st1[2]));
}
return b2;
}

public static void main(String args[]){
Book b[] = new Book[3];
b[0] =new Book(1,"Java", 100);
b[1] = new Book(2,"ADA", 200);
b[2] = new Book(3,"SP", 300);
//ArrayList a1 = new ArrayList();
TreeSet a1 = new TreeSet();
a1.add(new Book(2,"ADA", 200));
a1.add(new Book(2,"ADA", 200));
a1.add(new Book(1,"Java", 500));
a1.add(new Book(4,"MI", 800));
// Collections.sort(a1);
Iterator ir = a1.iterator();
while(ir.hasNext())
{
System.out.println((Book)(ir.next()));
}
System.out.print(a1);
/*System.out.print(Book.ObjToString(b));
String str1 = ObjToString(b);
Book c[] = StringToObj(str1);
for(int i=0;i<c.length;i++){
System.out.print(c[i]);
}*/
}
}

Array of Object to String and String to Array of Object

class Book{
int book_id;
String title;
float price;

Book(int id, String title, float price){
this.book_id = id;
this.title = title;
this.price = price;
}

public String toString(){
return this.book_id+"#"+this.title+"#"+this.price+"@";
}

public static String ObjToString(Book c[]){
String str = new String();
for(int i=0;i<c.length;i++){
str = str+c[i];
}
return str;
}

public static Book[] StringToObj(String str){
String[] st = str.split("@");
Book b2[] = new Book[st.length];

for(int i=0; i<st.length;i++){
String st1[] = st[i].split("#");
b2[i] = new Book(Integer.parseInt(st1[0]),st1[1],Float.parseFloat(st1[2]));
}
return b2;
}

public static void main(String args[]){

Book b[] = new Book[3];

b[0] =new Book(1,"Java", 100);
b[1] = new Book(2,"ADA", 200);
b[2] = new Book(3,"SP", 300);


System.out.print(Book.ObjToString(b));
String str1 = ObjToString(b);

Book c[] = StringToObj(str1);
for(int i=0;i<c.length;i++){
System.out.print(c[i]);
}
}
}

Demo Socket Program

Server Side :
import java.net.*;
import java.io.*;
class Server
{
public static void main(String s[]) throws Exception
{
ServerSocket ss=new ServerSocket(5000);
Socket s1=ss.accept();
System.out.println(s1);
DataOutputStream d1=new DataOutputStream(s1.getOutputStream());
d1.writeUTF("Hello Client");
d1.flush();
d1.close();
}
}


Client Side:
import java.net.*;
import java.io.*;
class Client
{
public static void main(String s[]) throws Exception
{
Socket ss=new Socket("localhost",5000);
DataInputStream d1=new DataInputStream(ss.getInputStream());
System.out.println(d1.readUTF());
d1.close();
}
}

Overloading in JAVA

Method Overloading and Constructor Overloading

/* 
Create Class Student with attributes like rollno and name, where You have to implement the concept of Method Overloading and Constructor Overloading.

Constructor Overloading and Static member concept :
Create Constructor in such a way where if User provide (rollno, name) then students object should be created with same. If user provide only name then next possible rollno should be assigned to student. 

Method Overloading :
Create method exam where marks of 3 subject will be supplied. If student get <50 marks in any of the subject then result should be declared as a Fail with no of ATKT in a exam.
If students appeared in exam of only one or two subject then using appropriate method should be called with different no of parameters 
*/


class Student
{
                int rollno;
                String name;
                static int noofstud;

// Constructor Overloading
                Student()
                {
                }

                Student(int roll,String sname)
                {
                                this.rollno=roll;
                                this.name=sname;
                                noofstud=roll+1;
                }
                Student(String sname)
                {
                                this.rollno=noofstud;
                                this.name=sname;
                }

//Method Overloading
                void exam(int sub1,int sub2,int sub3)
                {
                                int fail=0;
                                if(sub1<50)
                                {
                                                fail=fail+1;
                                }

                                if(sub2<50)
                                {
                                                fail=fail+1;
                                }

                                if(sub3<50)
                                {
                                                fail=fail+1;
                                }

                                if(fail==0)
                                {
                                                System.out.println("Congratulations "+ this.name + " You have successfully clear the Exam.....!");
                                }
                                else
                                {
                                                System.out.println("Sorry "+ this.name + " You are failed in "+fail+" Subject");
                                }
                }

                void exam(int sub1,int sub2)
                {
                                exam(sub1,sub2,0);
                }

                void exam(int sub1)
                {
                                exam(sub1,0,0);
                }

                public static void main(String args[])
                {
                                Student s1=new Student(1,"Ajay");
                                Student s2=new Student("Mitesh");
                                Student s3=new Student("Mitesh");
                                                               
                                s1.exam(50,60,12);
                                s2.exam(60,70);
                                s3.exam(30);
                }

}

Java Exception Handling

/ * 

Create a class Student with attributes roll no, name, age and course. Initialize values through parameterized constructor. If age of student is not in between 15 and 21 then generate user-defined exception “AgeNotWithinRangeException”. If name contains numbers or special symbols raise exception “NameNotValidException”. Define the two exception classes.

*/

import java.lang.Exception;
class AgeNotWithinRange extends Exception{
String msg; AgeNotWithinRange(String m) { 
this.msg = m; 
}
public String toString(){ 
return this.getClass().getName()+":"+msg;
}
}

class NameNotValidException extends Exception{
String msg; NameNotValidException(String m) { 
this.msg = m; 
}
public String toString(){ 
return this.getClass().getName()+":"+msg;
}
}

class Student{

 
int rollno;
int age;
String name;
String course;

Student(int r,String n, int a, String c) throws AgeNotWithinRange,NameNotValidException{ 
if(a < 15 || a > 21){  
throw new AgeNotWithinRange(String.valueOf(a)); 
} 
for(int i = 0;i < n.length();i++){  
if(!(Character.isLetter(n.charAt(i)))){      
throw new NameNotValidException(String.valueOf(i));  
} 
} 
this.rollno = r; 
this.name = n; 
this.age = a;
this.course = c; 
System.out.println("Object Created successfully.");
}

 public static void main(String args[]){

 
try{   
Student s1 = new Student(Integer.parseInt(args[0]),args[1],Integer.parseInt(args[2]),args[3]);
          }
catch(AgeNotWithinRange abc){ 
System.out.println(abc);  
}
catch(NameNotValidException def){ 
System.out.println(def);  
}
finally{  
System.out.println("Congratulation now you are UG.");
}

 
try{   
Student s2 = new Student(Integer.parseInt(args[4]),args[5],Integer.parseInt(args[6]),args[7]);
 }
catch(AgeNotWithinRange abc){ 
System.out.println(abc);  
}
catch(NameNotValidException def){ 
System.out.println(def);  
}
finally{  
System.out.println("Congratulation now you are PG.");
} 
}
}

Developed at : MCA - 3 (A), Department of MCA, B H Gardi College, Rajkot

Example of Multiple Inheritance in JAVA

abstract class A
{
            {
            System.out.println("Abstract Class Object is Created");
            }
           
abstract void display();
}

interface T
{
int i=10;
void print();
}

class B extends A implements T
{

void display()
            {
            System.out.println("Display from Derived Class");
            }

public void print()
            {
            System.out.println("Interface Implementation");
            }

public static void main(String s[])
            {
            B b=new B();
            b.display();
            b.print();
            System.out.println(T.i);
            }

}