Experiment:
1. Compile the above class (without main) using javac filename.java. Does it compile?
Why?
2. If above experiment is successful, run the compiled .class file. What happens? Why?
Program:
A sample DogTest class (in separate file)
public class Dog
{
public String name = "U";
public String bread = "pet";
public void speak()
{
System.out.println("Woof");
}
public void sleep()
{
System.out.println("Sleeping");
}
public void eat()
{
System.out.println("Eating");
}
}
//make new file and copy this code
public class Dog1
{
public static void main(String args[])
{
Dog ob1 = new Dog();
System.out.printf("My dog %s is:",ob1.name);
ob1.sleep();
System.out.printf("Hello %s \n:",ob1.name);
ob1.speak();
}
}
2nd programe:
Task 1: Write java code to create the GradeBook class that contains a displayMessage method
to displays a message on the screen. You will need to make an object of this class and call its
method to execute display the message.
Now declare a separate class that contains a main method. The GradeBookTest class declaration
will contain the main method that will control your application’s execution.
public class GradeBook
{
public void displayMassage()
{
System.out.println("Welcome to GradeBook");
}
}
//copy code then paste new file:
public class GradeBookTest
{
public static void main(String args[])
{
GradeBook d = new GradeBook();
d.displayMassage();
}
}
3rd programe:
Write a class Circle, which will model the functionality of a Circle.
1. Attributes
=> radius
2. Methods
=> calculateArea(): To compute area
=> calculatePerimeter(): To compute perimeter
public class Circle
{
public void calculateArea()
{
int r = 8;
double area = 3.14*r*r;
System.out.println("Area of circle :" +area);
}
public void calculatePerimeter()
{
int r = 8;
double perimeter = 2*3.14*3.14;
System.out.println("Perimeter of " +perimeter);
}
}
public class CircleTest
{
public static void main(String args[])
{
Circle c = new Circle();
c.calculateArea();
c.calculatePerimeter();
}
}
0 Comments