Header Ads Widget

Ticker

6/recent/ticker-posts

Practical 6 Java

PRACTICAL 6

Generics

1.      Declare a class InvoiceDetail which accepts a type parameter which is of type Number with following data members class InvoiceDetail { private String invoiceName; private N amount; private N Discount // write getters, setters and constructors } Call the methods in Main class.

public class Main {

public static void main(String[] args) {

InvoiceDetail<Float> o1=new InvoiceDetail<Float>(500.25F, 40F); InvoiceDetail<Integer> o2=new InvoiceDetail<Integer>(600,45); System.out.println(o1.getfinalamount()); System.out.println(o2.getfinalamount());
}}

class InvoiceDetail <N extends Number> { private String invoiceName;
private N amount; private N discount;
InvoiceDetail (N amount,N discount){ this.amount=amount; this.discount=discount;
}

double getfinalamount() { double sum=0;
sum=sum+amount.doubleValue()- amount.doubleValue()* discount.doubleValue()/100; return sum;
}

}

2) Implement Generic Stack.

import java.util.ArrayList; class stack<T> {
ArrayList<T> A; int top = -1;
int size; stack(int size){
this.size = size;
this.A = new ArrayList<T>(size);
}
void push(T X){
if (top + 1 == size) { System.out.println("Stack Overflow");
}
else {
top = top + 1;
if (A.size() > top)
A.set(top, X); else
A.add(X);
}
}
T top(){
if (top == -1) {
System.out.println("Stack Underflow"); return null;
}
else
return A.get(top);
}
void pop(){
if (top == -1) { System.out.println("Stack Underflow");
}
 
else

}
 

top--;
 

boolean empty() { return top == -1; } public String toString()
{
String Ans = "";
for (int i = 0; i < top; i++) {
Ans += String.valueOf(A.get(i)) + "->";
}
Ans += String.valueOf(A.get(top)); return Ans;
}
}
public class Main {
public static void main(String[] args) { stack<Integer> s1 = new stack<>(3);s1.push(10); s1.push(20);
s1.push(30);
System.out.println("s1 after pushing 10, 20 and 30 :\n" + s1); s1.pop();
System.out.println("s1 after pop :\n" + s1); stack<String> s2 = new stack<>(3); s2.push("wesehi");
s2.push("timepass"); s2.push("bored");
System.out.println("\ns2 after pushing 3 elements :\n" + s2); System.out.println("s2 after pushing 4th element :"); s2.push("GFG");
stack<Float> s3 = new stack<>(2); s3.push(100.0f);
s3.push(200.0f);
System.out.println("\ns3 after pushing 2 elements :\n" + s3); System.out.println("top element of s3:\n" + s3.top());
}
}

3) Write a program to sort the object of Book class using comparable and comparator interface. (Book class consistof book id, title,author and publisher as data members).


import java.io.*; import java.util.*;
public class Book implements comparable<Book> { private double book_id;
private String title; private String author; private String publisher;
Book (double book_id,String title,String author,String publisher) { this.book_id=book_id;
this.title=title; this.author=author; this.publisher=publisher;
}

public double getbid() { return book_id; } public String gettit() { return title; }
public String getauth() { return author;  }

}

import java.util.Comparator;

class idcompare implements Comparator<Book> { public int compare(Book m1, Book m2) {
if (m1.getbid() < m2.getbid()) return -1; if (m1.getbid() > m2.getbid()) return 1; else return 0;
}
 

}

public class titlecompare implements Comparator<Book> { public int compare(Book o1, Book o2) {
return o1.gettit().compareTo(o2.gettit());

}

}

public class Main {

public static void main(String[] args) { ArrayList<Book> list = new ArrayList<Book>();
list.add(new Book(3485374,"book by elon musk", "elon musk","vdcdhc")); list.add(new Book(5760956,"book fhf", "tkj;ohjgilf","rgohf"));
list.add(new Book(9760769,"book biotrugoref", "iojghf","trqewqfgs")); list.add(new Book(1793575,"book rthugj", "ngfduyh","reey")); idcompare ratingCompare = new idcompare();
Collections.sort(list, ratingCompare); for (Book movie: list){
System.out.println(movie.getbid() + " " +movie.gettit() + " " +movie.getauth());

}

titlecompare comparethetitle = new titlecompare(); Collections.sort(list, comparethetitle);
for (Book movie: list){

System.out.println(movie.getbid() + " " +movie.gettit() + " " + movie.getauth());

}

}

}


Post a Comment

0 Comments