Java

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

1.

Create a class Box that uses a parameterized method to initialize the dimensions of a
box. (Dimensions are width, height, depth of double type). The class should have a
method that can return volume. Obtain an object and print the corresponding volume
in main () function.

public class Box {


double h,w,d;

Box(double width,double height,double depth)


{
h=height;
w=width;
d=depth;
}
double volume()
{ double v;
v=h*w*d;
return v;
}

public static void main(String[] args) {

Box bc = new Box(8.5,80.3,9.6);


System.out.println(bc.volume());

2. Create a new class called “Calculator” which contains the following: 1. A static
method called powerInt(int num1,int num2) that accepts two integers and returns
num1 to the power of num2 (num1 power num2). 2. A static method called
powerDouble(double num1,int num2) that accepts one double and one integer and
returns num1 to the power of num2 (num1 power num2). 3. Call your method from
another class without instantiating the class (i.e. call it like Calculator.powerInt(12,10)
since your methods are defined to be static) Hint: Use Math.pow(double,double) to
calculate the power.

public class CALCI {

public static void main(String[] args) {

System.out.println(Calculator.powerDouble(85.0, 2));
System.out.println(Calculator.powerInt(85,3));
}

class Calculator
{
static double powerInt(int num1,int num2)
{
return Math.pow(num1,num2);
}
static double powerDouble(double num1,int num2)
{
return Math.pow(num1,num2);
}
}

or

import java.util.*;
import java.lang.*;
import java.io.*;

class Calculator{
static double power1;
static double power2;
static double powerInt(int num1,int num2)
{
power1=Math.pow(num1,num2);
return power1;
}
static double powerDouble(double num3,int num4)
{
power2=Math.pow(num3,num4);
return power2;
}
}
class Sample
{
public static void main (String[] args)
{
double p1=Calculator.powerInt(2,5);
double p2=Calculator.powerDouble(2.5,3);
System.out.println("The result of pwerInt:"+p1);
System.out.println("The result of pwerDouble:"+p2);

}
}

3. CCreate a class called Author is designed as follows:

It contains:
• Three private instance variables: name (String), email (String), and gender (char of either ‘m’ or ‘f’).
• One constructor to initialize the name, email and gender with the given values.

And, a class called Book is designed as follows:


It contains:
• Four private instance variables: name (String), author (of the class Author you have just created), price
(double), and qtyInStock (int). Assuming that each book is written by one author.
• One constructor which constructs an instance with the values given.
• Getters and setters: getName(), getAuthor(), getPrice(), setPrice(), getQtyInStock(), setQtyInStock(). Again there
is no setter for name and author.
Write the class Book (which uses the Author class written earlier).
Try:
1. Printing the book name, price and qtyInStock from a Book instance. (Hint: aBook.getName())
2. After obtaining the “Author” object, print the Author (name, email & gender) of the book.

import java.util.*;
import java.io.*;

class Author{
public static String email;
public static char gender;
public static String name;
Author(String n,String e,char g)
{
name=n;
email=e;
gender=g;
}

}
class Book{
String name1,author;
double price;
int qtyInStock;
Book(String n1)
{
name1=n1;

}
public void setPrice()
{
price=100;
}
public double getPrice()
{
return price;
}
public void setQtyInStock()
{
qtyInStock=10;
}
public int getQtyInStock()
{
return qtyInStock;
}
public String getName()
{
return name1;
}
public void getAuthor()
{
System.out.println("The name of the author is :"+Author.name);
System.out.println("The email id is :"+Author.email);
System.out.println("The gender is :"+Author.gender);
}

}
class Books{
public static void main(String args[])
{
Book b=new Book("Computer Networks");
Author a=new Author("amy","xxx.cse@rmd.ac.in",'f');
b.setPrice();
b.setQtyInStock();
System.out.println("The name of the book is :"+b.getName());
System.out.println("The price of the book is :"+b.getPrice());
System.out.println("The stock is :"+b.getQtyInStock());
b.getAuthor();

}
}

4.Search for a name


Write a program to accept an array of names and a name and check whether the name is present in the array. Return the count
of occurrence. Use the following array as input
{“Dave”, “Ann”, “George”, “Sam”, “Ted”, “Gag”, “Saj”, “Agati”, “Mary”, “Sam”, “Ayan”,
“Dev”, “Kity”, “Meery”, “Smith”, “Johnson”, “Bill”, “Williams”, “Jones”, “Brown”,
“Davis”, “Miller”, “Wilson”, “Moore”, “Taylor, “Anderson”, “Thomas”, “Jackson”}

import java.io.*;
import java.util.Scanner; class program1
{
public static void main(String args[])
{
String[] names={“Dave”, “Ann”, “George”, “Sam”, “Ted”, “Gag”, “Saj”, “Agati”, “Mary”,
“Sam”, “Ayan”, “Dev”, “Kity”, “Meery”, “Smith”, “Johnson”, “Bill”, “Williams”, “Jones”,
“Brown”, “Davis”, “Miller”, “Wilson”, “Moore”, “Taylor, “Anderson”, “Thomas”,
“Jackson”};
Scanner sr= new Scanner(System.in);
System.out.println("Enter a name to check");
String name=sr.nextLine();
int count=0;
for(int i=0;i<names.length;i++)
{
if(names[i].equals(name)) count++;
}
if(count!=0)
System.out.println("The Number of occurance of the Name is " + count );
else
System.out.println("Word not Found");
}
}

You might also like