1.Declare a class called author having author_name as private data member. Extend author class to have two sub classes called book_publication & paper_publication. Each of these classes have private member called title. Show usage of dynamic method dispatch (dynamic polymorphism) to display book or paper publications of a given author. Use command line arguments for inputting data.
Program:
class author{
private String name;
author(String nm){
name = nm;
}
void display(){
System.out.println("Author: "+name);
}
class book_pub extends author{
private String title;
book_pub(String tt){
super("");
title = tt;
}
void display(){
System.out.println("Book: "+title);
}
}
class paper_pub extends author{
private String title;
paper_pub(String tt){
super("");
title = tt;
}
void display(){
System.out.println("Paper: "+title);
}
}
class one{
public static void main(String args[]){
author o1 = new author(args[0]);
book_pub o2 = new book_pub(args[1]);
paper_pub o3 = new paper_pub(args[2]);
author r;
r = o1;
r.display();
r = o2;
r.display();
r = o3;
r.display();
}
}
OUTPUT:
2.Declare a class called book having author_name as private data member. Extend book class to have two sub classes called book_publication & paper_publication.Each of these classes have private member called title. Write a complete program to show usage of dynamic method dispatch (dynamic polymorphism) to display book or paper publications of given author.Use command line arguments for inputting data
Program:
class book {
protected String author_name;
public void get_aname(String k)
{
this.author_name = k;
}
public void display(){}
public void get_title(String k) {}
}
class book_publication extends book {
protected String title;
public void get_title(String k)
{
this.title=k;
}
public void display()
{
System.out.println("The book's author name is : " + author_name + " and the book title is : " + this.title );
}
}
class paper_publication extends book {
protected String title;
public void get_title(String k)
{
this.title=k;
}
public void display()
{
System.out.println("The paper's author name is : " + author_name + " and the paper title is : " + this.title );
}
}
public class prac1 {
public static void main(String args[])
{
book r=null;
book_publication b1 = new book_publication();
paper_publication p1 = new paper_publication();
r=b1;
r.get_aname(args[0]);
r.get_title(args[1]);
r.display();
r=p1;
r.get_aname(args[2]);
r.get_title(args[3]);
r.display();
}
}
OUTPUT:
3.The abstract vegetable class has three subclasses named Potato, Brinjal and Tomato. Write a java program that demonstrates how to establish this class hierarchy. Declare one instance variable of type String that indicates the color of a vegetable. Crete and display instances of these objects. Override the toString() method of object to return a string with the name of vegetable and its color.
Program:
abstract class Vegetable{
String color;
abstract public String toString();
}
class Potato extends Vegetable{
public String toString(){
color="Yellow";
return color;
}
}
class Brinjal extends Vegetable{
public String toString(){
color="Purple";
return color;
}
}
class Tomato extends Vegetable{
public String toString(){
color="Red";
return color;
}
}
class veg{
public static void main(String arg[]){
Vegetable r;
Potato a = new Potato();
Brinjal b = new Brinjal();
Tomato c = new Tomato();
r=a;
System.out.println("Color of Potato: "+a.toString());
r=b;
System.out.println("Color of Brinjal: "+b.toString());
r=c;
System.out.println("Color of Tomato: "+c.toString());
}
}
OUTPUT
private String name;
author(String nm){
name = nm;
}
void display(){
System.out.println("Author: "+name);
}
class book_pub extends author{
Program:
abstract class Vegetable{ String color; abstract public String toString(); } class Potato extends Vegetable{ public String toString(){ color="Yellow"; return color; } } class Brinjal extends Vegetable{ public String toString(){ color="Purple"; return color; } } class Tomato extends Vegetable{ public String toString(){ color="Red"; return color; } } class veg{ public static void main(String arg[]){ Vegetable r; Potato a = new Potato(); Brinjal b = new Brinjal(); Tomato c = new Tomato(); r=a; System.out.println("Color of Potato: "+a.toString()); r=b; System.out.println("Color of Brinjal: "+b.toString()); r=c; System.out.println("Color of Tomato: "+c.toString()); } }
OUTPUT
4.It is required to compute SPI (semester performance index) of n students of your college for their registered subjects in a semester. Declare a class called student having following data members:
-
id_no , no_of_subjects_registered, subject_code , subject_credits, grade_obtained and spi.
-
Define constructor and calculate_spi methods.
-
Define main to instantiate an array for objects of class student to process data of n students to be given as command line arguments.
4.It is required to compute SPI (semester performance index) of n students of your college for their registered subjects in a semester. Declare a class called student having following data members:
- id_no , no_of_subjects_registered, subject_code , subject_credits, grade_obtained and spi.
- Define constructor and calculate_spi methods.
- Define main to instantiate an array for objects of class student to process data of n students to be given as command line arguments.
Program:
import java.util.Scanner;
class student {
int id_no,no_of_subjects;
int subject_code[];
int subject_credits[];
int grade_obtained[];
double spi;
student(){}
student(int id,int num)
{
id_no = id;
no_of_subjects = num;
subject_code = new int[num];
subject_credits = new int[num];
grade_obtained = new int[num];
}
public void getdetails()
{
int i;
Scanner scan = new Scanner(System.in);
for(i=0;i<this.no_of_subjects;i++)
{
System.out.println("Enter detail for subject : " +(i+1));
System.out.print("Enter code : ");
this.subject_code[i]=scan.nextInt();
System.out.print("Enter credits : ");
this.subject_credits[i]=scan.nextInt();
System.out.print("Enter obtained grade : ");
this.grade_obtained[i]=scan.nextInt();
}
}
private void calculate()
{
int points;
points=0;
int credits=0;
int i;
for(i=0;i<this.no_of_subjects;i++)
{
points+=(this.subject_credits[i]*this.grade_obtained[i]);
credits+=this.subject_credits[i];
}
this.spi=(double)points/credits;
}
public void display()
{
calculate();
System.out.println("Hello ur ID is : " + id_no + " and you have scored : " + spi);
}
public void studentdet(int id, int num) {
id_no = id;
no_of_subjects = num;
subject_code = new int[num];
subject_credits = new int[num];
grade_obtained = new int[num];
}
}//ends class student
public class angle {
public static void main(String args[])
{
int count=Integer.parseInt(args[0]);
System.out.println("Hello.. :) You have selected to enter the details of " + count + " students!");
int i=0;
Scanner scan=new Scanner(System.in);
int id,num;
student sarray = null;
for(i=0;i<count;i++)
{
System.out.println("For student# " + (i+1) + "Enter details below!");
System.out.print("Enter ID number : ");
id=scan.nextInt();
System.out.print("Enter number of subjects : ");
num=scan.nextInt();
sarray=new student(id,num);
sarray.getdetails();
sarray.display();
}
}
}
OUTPUT
5.Write a method for computing xy doing repetitive multiplication. X and y are of type integer and are to be given as command line arguments.
Program:
class Power{
public static void main(String arg[]){
int x,y,z;
x = Integer.parseInt(arg[0]);
y = Integer.parseInt(arg[1]);
z=1;
for(int i=0;i<y;i++){
z=z*x;
}
System.out.println(x+"^"+y+": "+z);
}
}
OUTPUT
6.Describe abstract class called Shape which has three subclasses say Triangle, Rectangle and Circle. Define one method area() in the abstract class and override this area() in these three subclasses to calculate area for specific class’ object.
Program:
abstract class Shape{
double length,width,radius;
abstract double Area();
}
class Triangle extends Shape{
Triangle(double l,double b){
length=l;
width=b;
}
double Area(){
System.out.println("Area of Triangle: ");
return((length*width)/2);
}
}
class Rectangle extends Shape{
Rectangle(double l,double b){
length=l;
width=b;
}
double Area(){
System.out.println("Area of Rectangle: ");
return(length*width);
}
}
class Circle extends Shape{
Circle(double r){
radius=r;
}
double Area(){
System.out.println("Area of Circle: ");
return(3.14*(radius*radius));
}
}
class DemoShape{
public static void main(String arg[]){
Shape k;
Triangle t = new Triangle(5,10);
Rectangle r = new Rectangle(15,8);
Circle c = new Circle(6);
k=t;
System.out.println(k.Area());
k=r;
System.out.println(k.Area());
k=c;
System.out.println(k.Area());
}
}
OUTPUT
7.Write a program that creates and initializes a four integer element array. Calculate and display the average of its values.
Program:
class Array{
public static void main(String arg[]){
int a[] = {1,2,3,4};
int i,t=0,avg;
for(i=0;i<4;i++){
t = t + a[i];
}
avg = t/4;
System.out.println("Average of Array: "+avg);
}
}
OUTPUT
Program:
import java.util.Scanner;
class student {
int id_no,no_of_subjects;
int subject_code[];
int subject_credits[];
int grade_obtained[];
double spi;
student(){}
student(int id,int num)
{
id_no = id;
no_of_subjects = num;
subject_code = new int[num];
subject_credits = new int[num];
grade_obtained = new int[num];
}
public void getdetails()
{
int i;
Scanner scan = new Scanner(System.in);
for(i=0;i<this.no_of_subjects;i++)
{
System.out.println("Enter detail for subject : " +(i+1));
System.out.print("Enter code : ");
this.subject_code[i]=scan.nextInt();
System.out.print("Enter credits : ");
this.subject_credits[i]=scan.nextInt();
System.out.print("Enter obtained grade : ");
this.grade_obtained[i]=scan.nextInt();
}
}
private void calculate()
{
int points;
points=0;
int credits=0;
int i;
for(i=0;i<this.no_of_subjects;i++)
{
points+=(this.subject_credits[i]*this.grade_obtained[i]);
credits+=this.subject_credits[i];
}
this.spi=(double)points/credits;
}
public void display()
{
calculate();
System.out.println("Hello ur ID is : " + id_no + " and you have scored : " + spi);
}
public void studentdet(int id, int num) {
id_no = id;
no_of_subjects = num;
subject_code = new int[num];
subject_credits = new int[num];
grade_obtained = new int[num];
}
}//ends class student
public class angle {
public static void main(String args[])
{
int count=Integer.parseInt(args[0]);
System.out.println("Hello.. :) You have selected to enter the details of " + count + " students!");
int i=0;
Scanner scan=new Scanner(System.in);
int id,num;
student sarray = null;
for(i=0;i<count;i++)
{
System.out.println("For student# " + (i+1) + "Enter details below!");
System.out.print("Enter ID number : ");
id=scan.nextInt();
System.out.print("Enter number of subjects : ");
num=scan.nextInt();
sarray=new student(id,num);
sarray.getdetails();
sarray.display();
}
}
}
OUTPUT
5.Write a method for computing xy doing repetitive multiplication. X and y are of type integer and are to be given as command line arguments.
Program:
class Power{
public static void main(String arg[]){
int x,y,z;
x = Integer.parseInt(arg[0]);
y = Integer.parseInt(arg[1]);
z=1;
for(int i=0;i<y;i++){
z=z*x;
}
System.out.println(x+"^"+y+": "+z);
}
}
OUTPUT
6.Describe abstract class called Shape which has three subclasses say Triangle, Rectangle and Circle. Define one method area() in the abstract class and override this area() in these three subclasses to calculate area for specific class’ object.
Program:
abstract class Shape{
double length,width,radius;
abstract double Area();
}
class Triangle extends Shape{
Triangle(double l,double b){
length=l;
width=b;
}
double Area(){
System.out.println("Area of Triangle: ");
return((length*width)/2);
}
}
class Rectangle extends Shape{
Rectangle(double l,double b){
length=l;
width=b;
}
double Area(){
System.out.println("Area of Rectangle: ");
return(length*width);
}
}
class Circle extends Shape{
Circle(double r){
radius=r;
}
double Area(){
System.out.println("Area of Circle: ");
return(3.14*(radius*radius));
}
}
class DemoShape{
public static void main(String arg[]){
Shape k;
Triangle t = new Triangle(5,10);
Rectangle r = new Rectangle(15,8);
Circle c = new Circle(6);
k=t;
System.out.println(k.Area());
k=r;
System.out.println(k.Area());
k=c;
System.out.println(k.Area());
}
}
OUTPUT
7.Write a program that creates and initializes a four integer element array. Calculate and display the average of its values.
Program:
class Array{
public static void main(String arg[]){
int a[] = {1,2,3,4};
int i,t=0,avg;
for(i=0;i<4;i++){
t = t + a[i];
}
avg = t/4;
System.out.println("Average of Array: "+avg);
}
}
OUTPUT
No comments:
Post a Comment