Thursday, August 13, 2020

write a java program for Hybrid inheritance

 class Hybri 

{

int bonus=2000,sal=5000;

}

class chlid1 extends Hybri

{

void showchild1()

{

System.out.println("bonus"+bonus+"sal"+sal);

}

};

class chlid2 extends Hybri

{

void showchild2()

{

System.out.println("bonus"+bonus+"sal"+sal);

}

};

class Driver1

{

public static void main(String[] args) 

{

chlid1 c1=new chlid1();

        chlid2 c2=new chlid2();

c1.showchild1();

c2.showchild2();

}

}


super class private varibales can't access in child

 class Priva  //super class private varibales can't access in child

{

int a=10;

private int b=20;

}

class Use extends Priva //chlid

int c=20;

}

class Dri

{


public static void main(String[] args) 

{

Use p1=new Use();

System.out.println("public variable"+p1.a);

       System.out.println("private variable"+p1.b);

      System.out.println("private variable"+p1.c);

}

}


write a java program for character variable increment in java

 class D1 

{

public static void main(String[] args) 

{

char ch='X';

ch++;

System.out.println(ch);

}

}


write a java program for character variable Decrement in java

 class D1 

{

public static void main(String[] args) 

{

char ch='X';

ch--;

System.out.println(ch);

}

}

Print the average of three numbers given by user by creating a class named 'Average' having a method to calculate and print the average.

 class  Average

{

int a=5,b=4,c=3;

float avg;

void calaverage()

{

avg=(a+b+c)/3;

}

void printavg()

{

System.out.println("Average of three varibales"+avg);

}

public static void main(String[] args) 

{

    Average a1=new Average();

     a1.calaverage();

a1.printavg();

}

}


•Write a Java class Book with following features:

 

Write a Java class Book with following features:
Instance variables :
title for the title of book of type String.
author for the author’s name of type String.
price for the book price of type double.
Instance methods:
public void setTitle(String title): Used to set the title of book.
public void setAuthor(String author): Used to set the name of author of book.
public void setPrice(double price): Used to set the price of book.
public String getTitle(): This method returns the title of book.
public String getAuthor(): This method returns the author’s name of book.
Public double getPrice( ): Used to set the price of book.
class Book 
{
String title,author;
double price;
void setTitle(String t)
{
title=t;
}
void setAuthor(String a)
{
author=a;
}
void setPrice(double p)
{
price=p;
}
String getTitle()
{
return title;
}
String getAuthor()
{
return author;
}
double getPrice()
{
return price;
}
public static void main(String[] args) 
{
Book b1=new Book();
b1.setTitle("introduction to java");
b1.setAuthor("Dietiel");
b1.setPrice(550.60);
System.out.println("Book Title   "+ b1.getTitle()+"Author   "+b1.getAuthor()+"Price   "+b1.getPrice());
}
}

Write a Java class Author with following features:

 

Write a Java class Author with following features:
Instance variables :
firstName for the author’s first name of type String.
lastName for the author’s last name of type String.
Instance methods:
public void setFirstName (String firstName): Used to set the first name of author.
public void setLastName (String lastName): Used to set the last name of author.
public String getFirstName(): This method returns the first name of the author.
public String getLastName(): This method returns the last name of the author.


class Author 

{

String firstname,lastname;

void setfirstname(String first)

{

firstname=first;

}

void setlastname(String last)

{

lastname=last;

}

String getfirstname()

{

return firstname;

}

String getlastname()

{

return lastname;

}

public static void main(String[] args) 

{

Author a1=new Author ();

a1.setfirstname("john");

a1.setlastname("k");

System.out.println("FirstName is"+a1.getfirstname()+"Last Nameis"+a1.getlastname());

}

}