Finals For Data Structure
Finals For Data Structure
import java.io.*;
/**
*
* @author User
*/
public class Array1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException{
// TODO code application logic here
BufferedReader DataIn=new BufferedReader(new InputStreamReader(System.in));
double []myList = {1.9,2.9,3.4,3.5};
}
//Summing all elements
double total = 0;
for (int i=0;i<myList.length;i++){
total+=myList[i];
}
System.out.print("Total is" +total);
//Finding the largest element
double max=myList[0];
for (int i=1; i<myList.length;i++){
if (myList[i]>max)max=myList[i];
}
System.out.println("Msx is"+max);
}
}
public class Singlearray1 {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException{
// TODO code application logic here
BufferedReader DataIn=new BufferedReader(new InputStreamReader(System.in));
double []avg=new double[5];
double []prelim=new double[5];
double []midterm=new double[5];
double []Final=new double[5];
int x;
for(x=0;x<=2;x++){
System.out.print("enter prelim grade:");
prelim[x]=Double.parseDouble(DataIn.readLine());
System.out.print("enter midterm grade:");
midterm[x]=Double.parseDouble(DataIn.readLine());
System.out.print("enter final grade:");
Final[x]=Double.parseDouble(DataIn.readLine());
}
for(x=0;x<=2;x++){
avg[x]=(prelim[x]+midterm[x]+Final[x])/3;
}
for(x=0;x<=2;x++){
System.out.println("The Average:"+avg[x]);
}
}
package secondsem;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author User
*/
public class Secondsem {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
BufferedReader DataIn=new BufferedReader (new InputStreamReader(System.in));
double grade[][][]=new double[3][3][2];
double id[]=new double[3];
int R,C,A;
total [R]=(grade[R][0][0]+grade[R][1][0])/3;
}
for(R=0; R<=2; R++){
total1[R]=(grade[R][1][1]+grade[R][2][1])/3;
}
for (R=0; R<=2; R++){
total1[R]=(grade[R][1][1]+grade[R][2][1])/3;
}
}
}
package dimentionalarray;
/**
*
* @author User
*/
public class DimentionalArray {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
arr[i][j][k]= num;
num++;
}
}
}
for(i=0; i<3; i++){
for(j=0; j<4; j++){
for(k=0; k<2; k++){
System.out.println("arr [" +i+ "] [" +j+ "] [" +k+ "] +" +arr[i][j][k]+ "\t");
}
System.out.println();
}
System.out.println();
}
}
}
package base;
/**
*
* @author User
*/
public class Base {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
boolean flag = true;
char ch ='A';
byte b=12;
short s= 24;
int i=257;
long l=890L;//note the use of "L" here
float f = 3.1415F; //note the use of "F" here
double d=2.1828;
System.out.println("flag="+flag); //the "++ is string concatation
System.out.println("ch="+ch);
System.out.println("b="+b);
System.out.println("s="+s);
System.out.println("i="+i);
System.out.println("l="+l);
System.out.println("f="+f);
System.out.println("d="+d);
}
package sales;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author User
*/
public class Sales {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
BufferedReader DataIn=new BufferedReader (new InputStreamReader(System.in));
double[][]sale=new double [5][3];
double[] avg=new double [5];
double[] total=new double [5];
double tempavg,temptotal;
int R,C;
for (R=0; R<=4; R++){
for (C=0; C<=2; C++){
System.out.print("Enter A Sale:");
sale[R][C]=Integer.parseInt(DataIn.readLine());
}
}
for (R=0; R<=4; R++){
total[R]=sale[R][0]+sale[R][1]+sale[R][2];
}
for(R=0; R<=4; R++){
avg[R]=total[R]/3;
}
for(R=0; R<=3; R++){
for (C=R+1; C<=4; C++){
if(avg[R]<avg[C]){
if(total[R]<total[C]){
tempavg=avg[R];
avg[R]=avg[C];
avg[C]=tempavg;
temptotal=total[C];
total[R]=total[C];
total[C]=temptotal;
}
}
}
}
System.out.println("\tSALES\t\t"+"TOTAL\t\t"+"AVERAGE");
for(R=0; R<=4; R++){
System.out.println(sale[R][0]+" "+sale[R][1]+" "+sale[R][1]+" "+sale[R][2]+" "+total[R]+" "+avg[R]);
}
}
}
MIDTERM EXAM
package studentmid;
import java.io.*;
import java.util.Stack;
/**
*
* @author User
*/
public class StudentMid {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
BufferedReader DataIn=new BufferedReader (new InputStreamReader(System.in));
Stack<String> totalgrade= new Stack<>();
int count, i;
float totalGrade = 0, average,CurretnSatck=0;
int x;
}
}
package doublearray11;
import java.io.*;
* @author User
*/
public class Doublearray11 {
* @param args the command line arguments
*/
public static void main(String[] args)throws IOException {
// TODO code application logic here
BufferedReader DataIn = new BufferedReader (new InputStreamReader(System.in));
double[]avg=new double[5];
double[][]grade=new double[5][3];
int r,c;
for(r=0;r<=4;r++){
for(c=0;c<=1;c++){
System.out.print("Enter Student Grade:");
grade[r][c]=Double.parseDouble(DataIn.readLine());
}
}
for(r=0;r<=4;r++){
avg[r]=(grade[r][0]+grade[r][1]+grade[r][2])/3;
}
for(r=0;r<=4;r++){
System.out.println("Average:"+avg[r]);
}
}
package activity5.average;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author User
*/
public class Activity5Average {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args)throws IOException {
// TODO code application logic here
BufferedReader DataIn=new BufferedReader (new InputStreamReader(System.in));
double[] avg=new double [5];
double[][] sales=new double [5][3];
int S,A;
System.out.print("Enter sales:");
sales[S][A]=Double.parseDouble(DataIn.readLine());
}
}
for(S=0;S<=4;S++){
avg[S]=(sales[S][2]+sales[S][0]+sales[S][1])/3;
}
for(S=0;S<=4;S++){
System.out.println("Average:"+avg[S]);
}
}
package pkg3darray;
import java.io.*;
import java.util.Arrays;
/**
*
* @author User
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException{
// TODO code application logic here
BufferedReader girlie = new BufferedReader (new InputStreamReader(System.in));
int iArr[]={2,1,9,6,4};
for(int number:iArr){
System.out.println("Number="+number);
}
Arrays.sort(iArr);
System.out.println("The Sorted int array is: ");
for(int number: iArr){
System.out.println("Number ="+number);
}
}
}
package cards;
import java.io.*;
import java.util.Stack;
/**
*
* @author User
*/
public class Cards {
/**
* @param args the command line arguments
*/
public static void main(String[] args)throws IOException {
// TODO code application logic here
BufferedReader DataIn=new BufferedReader (new InputStreamReader(System.in));
//Creating a Stack
Stack<String> stackOfCards = new Stack<>();
}
package stacksizeexample;
import java.util.Stack;
import java.util.Scanner;
/**
*
* @author User
*/
public class Stacksizeexample {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int count, i;
float totalMarks = 0, percentage, average;
Scanner scanner;
scanner = new Scanner(System.in);
/**
*
* @author User
*/
public class MyStack {
private int maxSize;
private long[] stackArray;
private int top;
public MyStack(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(long j) {
stackArray[++top] = j;
}
public long pop() {
return stackArray[top--];
}
public long peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MyStack theStack = new MyStack(10);
theStack.push(10);
theStack.push(20);
theStack.push(30);
theStack.push(40);
theStack.push(50);
while (!theStack.isEmpty()) {
long value = theStack.pop();
System.out.print(value);
System.out.print(" ");
}
System.out.println("");
}
}
package stackproducts;
import java.io.*;
import java.util.Stack;
/**
*
* @author User
*/
public class Stackproducts {
/**
* @param args the command line arguments
*/
public static void main(String[] args)throws IOException {
// TODO code application logic here
BufferedReader DataIn=new BufferedReader (new InputStreamReader(System.in));
Stack<String> stackOfProducts = new Stack<>();
int x;
for(x=1;x<=10;x++){
System.out.println("Stack : " + stackOfProducts);
stackOfProducts.push(DataIn.readLine());
}
for(x=1;x<=10;x++){
String productAtTop = stackOfProducts.pop();
System.out.println("Stack.pop() => " + productAtTop);
System.out.println("Current Stack => " + stackOfProducts);
System.out.println();
}
}
package demo1;
import java.util.*;
public class Demo1 {
static void showpush(Stack stack1, int a) {
stack1.push(new Integer(a));
System.out.println("push(" + a + ")");
System.out.println("stack: " + stack1);
}
static void showpop(Stack stack1) {
Integer a = (Integer) stack1.pop();
System.out.println(a);
System.out.println("stack: " + stack1);
}
public static void main(String[] args) {
// TODO code application logic here
Stack stack1 = new Stack();
System.out.println("stack: " + stack1);
showpush(stack1, 40);
showpush(stack1, 50);
showpush(stack1, 60);
showpush (stack1, 70);
showpush (stack1, 80);
showpush (stack1, 90);
showpush (stack1, 100);
showpop(stack1);
showpop(stack1);
showpop(stack1);
showpop(stack1);
showpop(stack1);
showpop(stack1);
showpop(stack1);
try {
showpop(stack1);
} catch (EmptyStackException e) {
System.out.println("it Is Empty Stack");
}
}
}
package iteratoroverstackexam;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;
/**
*
* @author User
*/
public class IteratorOverStackExam {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Stack<String> stackOfPlates = new Stack<>();
stackOfPlates.add("Plate 1");
stackOfPlates.add("Plate 2");
stackOfPlates.add("Plate 3");
stackOfPlates.add("Plate 4");
System.out.println("\n=== Iterate over a Stack using iterator() and Java 8 forEachRemaining() method ===");
platesIterator = stackOfPlates.iterator();
while (platesIterator.hasNext()) {
String plate = platesIterator.next();
System.out.println(plate);
}
System.out.println("\n=== Iterate over a Stack from TOP to BOTTOM using listIterator() ===");
// ListIterator allows you to traverse in both forward and backward directions.
// We'll start from the top of the stack and traverse backwards.
ListIterator<String> platesListIterator = stackOfPlates.listIterator(stackOfPlates.size());
while (platesListIterator.hasPrevious()) {
String plate = platesListIterator.previous();
System.out.println(plate);
}
}
}
package utilarrays;
import java.util.Arrays;
import java.io.*;
/**
*
* @author User
*/
public class Utilarrays {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
BufferedReader DataIn=new BufferedReader (new InputStreamReader(System.in));
// TODO code application logic here
int r,c,a;
int sale[][][]=new int[3][5][3];
int first_total[]= new int[5];
int sec_total[]= new int[5];
int third_total[]= new int[5];
//input
for (r=0; r<=2; r++){
System.out.print("\n\n-----Arrays===Arrays===Arrays-----\n\n");
for (c=0; c<=4; c++){
System.out.print("***I love JAVA PROGRAMMING***\n");
for (a=0; a<=2; a++){
System.out.print("Enter the Sale: ");
sale[r][c][a]=Integer.parseInt(DataIn.readLine());
}
}
}
//proccess
for (c=0; c<=4; c++){
first_total[c]=(sale[0][c][0]+sale[1][c][0]+sale[2][c][0]);
}
for (c=0; c<=4; c++){
sec_total[c]=(sale[0][c][1]+sale[1][c][1]+sale[2][c][1]);
}
for (c=0; c<=4; c++){
third_total[c]=(sale[0][c][2]+sale[1][c][2]+sale[2][c][2]);
}
int iarr[]={first_total[0],first_total[1],first_total[2],first_total[3],first_total[4]};
System.out.println("\n***************(CBAA department)*************");
System.out.println("\nNot sorted:");
for (int number : iarr){
System.out.println("Total: " + number);
}
//sorting array
Arrays.sort(iarr);
int iarr1[]={sec_total[0],sec_total[1],sec_total[2],sec_total[3],sec_total[4]};
System.out.println("\n***************(CCJE department)*************");
System.out.println("\nNot sorted:");
for (int number1 : iarr1){
System.out.println("Total: " + number1);
}
//sorting array
Arrays.sort(iarr1);
int iarr2[]={third_total[0],third_total[1],third_total[2],third_total[3],third_total[4]};
System.out.println("\n***************(ICS department)*************");
System.out.println("\nNot sorted:");
for (int number2 : iarr2){
System.out.println("Total: " + number2);
}
//sorting array
Arrays.sort(iarr2);
}
}
}
package testcollection;
import java.util.LinkedList;
import java.util.Queue;
import java.util.PriorityQueue;
import java.util.Iterator;
/**
*
* @author User
*/
public class TestCollection {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
PriorityQueue<String>queue=new PriorityQueue<String>();
queue.add("Amit");
queue.add("Vijay");
queue.add("Karan");
queue.add("Jai");
queue.add("Rahul");
queue.add("Karan");
System.out.println("head:" + queue.element());
System.out.println("iterating the queue elements:");
Iterator itr=queue.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
queue.remove();
queue.poll();
System.out.println("after removing two elements:");
Iterator<String>itr2=queue.iterator();
while(itr2.hasNext()){
System.out.println(itr2.next());
}
}
}
package queuegradessample;
import java.util.LinkedList;
import java.util.Queue;
import java.io.*;
/**
*
* @author User
*/
public class QueueGrades {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException{
// TODO code application logic here
BufferedReader gb=new BufferedReader(new InputStreamReader(System.in));
Queue<Integer> q = new LinkedList<>();
q.add(6);
q.add(1);
q.add(8);
q.add(4);
q.add(7);
System.out.println("The queue is: " + q);
int num1 = q.remove();
System.out.println("The element deleted from the head is: " + num1);
System.out.println("The queue after deletion is: " + q);
int head = q.peek();
System.out.println("The head of the queue is: " + head);
int size = q.size();
System.out.println("The size of the queue is: " + size);
}
package queuegrades;
import java.util.LinkedList;
import java.util.Queue;
import java.io.*;
/**
*
* @author User
*/
public class QueueGrades {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws
IOException{
// TODO code application logic here
BufferedReader gb=new BufferedReader
(new InputStreamReader(System.in));
Queue<Integer> q = new LinkedList<>();
int grades,i,avg,total=0;
for (i=1;i<=10;i++){
System.out.print("Enter Grades:");
grades=Integer.parseInt(gb.readLine());
q.add(grades);
if(i==10){
System.out.println("Grades:"+q);
}
}
for(i=1;i<=5;i++){
int num=q.remove();
System.out.println("Remove"+i+"values:"+num);
System.out.println("Remaining"+num);
}
}
}
class Emloyee{
int idnum;
String ename;
static String company="TNTS";
}
void display (){System.out.println(idnum+"\t"+ename+"\t"+company);}
e1.display ();
e2.display ();
}
}
class emloyee2{
int idnum;
String ename;
static String company="TNTS";
}
void display (){System.out.println(idnum+" "+ename+" "+company);
}
e1.display ();
e2.display ();
e3.display ();
}
}
class employee3{
static{System.out.println("Static block is performed..");
}
public static void main (String args[]){
System.out.println("Completed!");
}
}
// ReportCard.java
//Class declaration with a method that has a parameter
import java.util.Scanner;
//SquareTest.java
//Application to test class Square.
}
//save as SampleDefaultB.java
package mypack;
import pack.*;
class SampleDefaultB{
public static void main (String args []){
SampleDefaultA obj = new SampleDefaultA();//Compile Time Error
obj.msg();//Compile Time Error
}
}
//sample as Sample_PublicA.java
package pack;
public class SamplePublicA{
public void msg(){System.out.println("Sikam Gayam");}
}
//save as SAmplePublicB.java
package mypack;
import pack.*;
class SamplePublicB{
public static void main(String args []){
SamplePublicA obj =new SamplePublicA();
obj.msg();
}
}
//save as SampleProtectedA.java
package pack;
public class SampleProtectedA{
protected void msg(){System.out.println("Sikam Gayam");}
}
//save by SampleProtectedB.java
package mypack;
import pack.*;
}
public class SamplePrivate{
public static void main (String args[]){
X obj=new X();
System.out.println(obj.data);//Compile Time Error
obj.msg(); //Compile Time Error
}
}