HCI NEW

HCI NEW

Postby sin31423 » Mon Nov 04, 2019 6:14 am

JAVA ARRAY
Program to implement jagged array
// Program to demonstrate 2-D jagged array in Java class Main
{
public static void main(String[] args)
{
// Declaring 2-D array with 2 rows int arr[][] = new int[2][];
} }
// Making the above array Jagged
// First row has 3 columns arr[0] = new int[3];
// Second row has 2 columns arr[1] = new int[2];
// Initializing array
int count = 0;
for (int i=0; i<arr.length; i++)
for(int j=0; j<arr[i].length; j++) arr[i][j] = count++;
// Displaying the values of 2D Jagged array System.out.println("Contents of 2D Jagged Array"); for (int i=0; i<arr.length; i++)
{
for (int j=0; j<arr[i].length; j++) System.out.print(arr[i][j] + " ");
System.out.println(); }
JAVA STRING
Java program to print distinct characters of a string.
public class GFG {
static final int NO_OF_CHARS = 256;
/* Print duplicates present in the passed string */ static void printDistinct(String str)
{
// Create an array of size 256 and count of // every character in it
int[] count = new int[NO_OF_CHARS];
/* Count array with frequency of characters */ int i;
for (i = 0; i < str.length(); i++)
if(str.charAt(i)!=' ')
count[(int)str.charAt(i)]++;
// Print characters having count more than 0
int n = i;

}
for (i = 0; i < n; i++)
if (count[(int)str.charAt(i)] == 1)
System.out.print(str.charAt(i));
/* Driver program*/
public static void main(String args[])
{
String str = "GeeksforGeeks";
printDistinct(str); }
}
JAVA CLASSES AND OBJECTS
// Class Declaration
public class Dog
{
// Instance Variables
String name; String breed; int age; String color;
// Constructor Declaration of Class public Dog(String name, String breed,
int age, String color)
{
this.name = name;
this.breed = breed; this.age = age; this.color = color;
}
// method 1
public String getName()
{
return name;
}
// method 2
public String getBreed()
{
return breed;
}
// method 3 public int getAge()
{
return age;
}
// method 4
public String getColor()
{
return color;
} @Override

public String toString()
{
return("Hi my name is "+ this.getName()+
}
".\nMy breed,age and color are " + this.getBreed()+"," + this.getAge()+ ","+ this.getColor());
public static void main(String[] args)
{
Dog tuffy = new Dog("tuffy","papillon", 5, "white");
System.out.println(tuffy.toString()); }
}
JAVA INHERITANCE
class Bicycle is a base class, class MountainBike is a derived class which extends Bicycle class and class Test is a driver class to run program.
//Java program to illustrate the // concept of inheritance
// base class class Bicycle
{
// the Bicycle class has two fields public int gear;
public int speed;
// the Bicycle class has one constructor public Bicycle(int gear, int speed)
{
this.gear = gear;
this.speed = speed; }
// the Bicycle class has three methods public void applyBrake(int decrement)
{
speed -= decrement;
}
public void speedUp(int increment)
{
speed += increment;
}
// toString() method to print info of Bicycle public String toString()
{
return("No of gears are "+gear
} }
+"\n"
+ "speed of bicycle is "+speed);
// derived class
class MountainBike extends Bicycle

{
// the MountainBike subclass adds one more field public int seatHeight;
// the MountainBike subclass has one constructor public MountainBike(int gear,int speed,
int startHeight)
{
// invoking base-class(Bicycle) constructor super(gear, speed);
seatHeight = startHeight;
}
// the MountainBike subclass adds one more method public void setHeight(int newValue)
{
seatHeight = newValue;
}
// overriding toString() method // of Bicycle to print more info @Override
public String toString()
{
return (super.toString()+
} }
// driver class public class Test
"\nseat height is "+seatHeight);
{
public static void main(String args[]) {
MountainBike mb = new MountainBike(3, 100, 25); System.out.println(mb.toString());
} }
JAVA ABSTRACT CLASS AND METHOD
JAVA INTERFACE
import java.io.*;
// A simple interface interface in1
{
// public, static and final
final int a = 10;
// public and abstract
void display(); }

// A class that implements interface. class testClass implements in1
{
// Implementing the capabilities of // interface.
public void display()
{
System.out.println("Geek"); }
// Driver Code
public static void main (String[] args)
{
testClass t = new testClass();
t.display();
System.out.println(a); }
}
JAVA PACKAGES
Vehicle.java
package vehicles; interface Vehicle
{
public void run();
public void speed(); }
Cars.java
package vehicles;
public class Car implements Vehicle
{
public void run()
{
System.out.println("Car is running.");
}
public void speed()
{
System.out.println("Speed of Car: 50 Km/h");
}
public static void main(String args[])
{
Car Car = new Car();
Car.run();
Car.speed(); System.out.println("Hello World!");
}
}
JAVA EXCEPTION HANDLING
// Java program to demonstrate exception is thrown // how the runTime system searches th call stack
// to find appropriate exception handler.

class ExceptionThrown
{
// It throws the Exception(ArithmeticException).
// Appropriate Exception handler is not found within this method. static int divideByZero(int a, int b){
// this statement will cause ArithmeticException(/ by zero) int i = a/b;
return i; }
// The runTime System searches the appropriate Exception handler // in this method also but couldn't have found. So looking forward // on the call stack.
static int computeDivision(int a, int b) {
int res =0;
try
{
res = divideByZero(a,b);
}
// doesn't matches with ArithmeticException catch(NumberFormatException ex)
{
System.out.println("NumberFormatException is occured"); }
return res;
}
// In this method found appropriate Exception handler. // i.e. matching catch block.
public static void main(String args[]){
int a = 1; int b = 0;
try {
int i = computeDivision(a,b); }
// matching ArithmeticException catch(ArithmeticException ex)
{
// getMessage will print description of exception(here / by zero)
System.out.println(ex.getMessage()); }
} }
JAVA MULTI THREADING
// Java code for thread creation by extending // the Thread class
class MultithreadingDemo extends Thread
{

} }
{
// Displaying the thread that is running System.out.println ("Thread " +
Thread.currentThread().getId() + " is running");
}
catch (Exception e)
{
// Throwing an exception
System.out.println ("Exception is caught"); }
public void run()
{
try
// Main Class
public class Multithread
{
public static void main(String[] args)
{
int n = 8; // Number of threads for (int i=0; i<8; i++)
{
} }
MultithreadingDemo object = new MultithreadingDemo();
object.start(); }
JAVA FILE HANDLING
1. Java Program to Reverse the Contents of a File and Print it.
import java.util.*; import java.io.*;
class File2{
static String reverse(String s)
{
char[] c=s.toCharArray();
int n=s.length(); String ss="";
for(int i=n-1;i>=0;i--) {
ss+=c[i];} return ss;
}
public static void main(String args[]) throws IOException {
try{
String s1="";
FileInputStream fstream=new FileInputStream("reverse.txt"); DataInputStream in=new DataInputStream(fstream);
BufferedReader br=new BufferedReader(new InputStreamReader(in));

String strline; while((strline=br.readLine())!=null)
{
s1+=strline;
} System.out.println("Original : "+s1);
System.out.println("Reversed : "+ reverse(s1)); } catch(Exception e)
{
System.out.println("Exception found: "+e);}
}}
JAVA COLLECTION FRAMEWORK
1. ARRAYLIST
import java.util.*;
class TestJavaCollection1{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist list.add("Ravi");//Adding object in arraylist
list.add("Vijay");
list.add("Ravi");
list.add("Ajay");
//Traversing list through Iterator
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
2. LINKED LIST
import java.util.*;
public class TestJavaCollection2{
public static void main(String args[]){ LinkedList<String> al=new LinkedList<String>(); al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator<String> itr=al.iterator(); while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
3. SET
import java.util.*;
public class TestJavaCollection7{ public static void main(String args[]){ //Creating HashSet and adding elements

HashSet<String> set=new HashSet<String>(); set.add("Ravi");
set.add("Vijay");
set.add("Ravi");
set.add("Ajay");
//Traversing elements Iterator<String> itr=set.iterator(); while(itr.hasNext()){ System.out.println(itr.next());
}
}
}
4. MAP
import java.util.*; class MapExample2{
public static void main(String args[]){
Map<Integer,String> map=new HashMap<Integer,String>(); map.put(100,"Amit");
map.put(101,"Vijay");
map.put(102,"Rahul");
//Elements can traverse in any order
for(Map.Entry m:map.entrySet()){
System.out.println(m.getKey()+" "+m.getValue()); }
} }
JAVA JDBC
1. Create a student table which contains the name, reg.no, marks of 3 subjects. Retrieve the student details whose average is above 90 and display the student details who got the first rank. (do SQL things in SQL console and retrieve using java)

import java.sql.*; class mysqlcon {
public static void main(String args[]) { try
} }
{ Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost: 3306/scse","scse","scse");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getInt(3)+" "+rs.getInt(4)+" "+rs.getInt(5)); con.close(); }
catch(Exception e){ System.out.println(e);}
sin31423
 
Posts: 2
Joined: Mon Oct 29, 2018 3:09 am
Reputation: 0

Return to Programming and Algorithms



Who is online

Users browsing this forum: No registered users and 1 guest