0% found this document useful (0 votes)
2 views

Java Tutorial 2 Answer

The document provides a comprehensive Java tutorial covering object instantiation, class creation, and examples of methods such as checking for prime numbers and generating Fibonacci series. It includes code snippets demonstrating the creation and use of classes, methods for calculating area and perimeter of a circle, and calculating the sum and average of a list of numbers. Additionally, it explains the differences between classes and objects, and includes sample outputs for various code snippets.

Uploaded by

Rithick Roshan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Tutorial 2 Answer

The document provides a comprehensive Java tutorial covering object instantiation, class creation, and examples of methods such as checking for prime numbers and generating Fibonacci series. It includes code snippets demonstrating the creation and use of classes, methods for calculating area and perimeter of a circle, and calculating the sum and average of a list of numbers. Additionally, it explains the differences between classes and objects, and includes sample outputs for various code snippets.

Uploaded by

Rithick Roshan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

JAVA TUTORIAL 2 ANSWER

1. How do you create an instance (object) of a class in


Java? Provide an example of object instantiation.
In Java, you create an instance (object) of a class by using
the new keyword followed by the class constructor. The
constructor is a special method within the class that initializes the
object's state. Here's the basic syntax for creating an object:

ClassName objectName = new ClassName();

Here's an example of creating an object of a simple class called


Person

public class Person {

String name;

int age;

// Constructor

public Person(String name, int age) {

this.name = name;

this.age = age;

public void introduce() {

System.out.println("Hello, my name is " + name + " and I am


" + age + " years old.");

public static void main(String[] args) {


// Creating an object of the Person class

Person person1 = new Person("Alice", 30);

// Calling the introduce method on the object

person1.introduce();

// Creating another object of the Person class

Person person2 = new Person("Bob", 25);

// Calling the introduce method on the second object

person2.introduce();

2. Write the syntax for creating a class. Give an example.


In Java, you create a class using the following syntax:
accessModifier class ClassName {
// Fields (variables)

// Constructors

// Methods
}
Example;
package sides;

public class car {


//instane variable
String color;
int price;

//constructor
public car(String color,int price) {
this.color=color;
this.price=price;
}
//methode
public void print()
{
System.out.println("this car is"+" "+color+" it price is"+"
"+price);
}
public static void main(String[] args)
{
car car1=new car("red",500);
car1.print();
car car2=new car("green",700);
car2.print();

3. Write a program to check weather given number is


prime or not.
4. package sides;
5. import java.util.*;
6.
7. public class Prime {
8.
9. public static boolean isprime (int num){
10. if(num==0||num==1)
11. {
12. return false;
13. }
14. for(int i=2;i<=Math.sqrt(num);i++) {
15. if (num%i==0)
16. {
17. return false;
18. }
19. }
20. return true;
21. }
22.
23. public static void main(String[] args) {
24.
25. int data;
26. Scanner scan=new Scanner(System.in);
27. data=scan.nextInt();
28. if (isprime(data)==true)
29. {
30. System.out.println("It is prime number");
31. }
32. else
33. {
34. System.out.println("Not prime number");
35. }
36. }
37.
38. }
4.Difference between class and object.
Class Object
Class is the blueprint of an
object. It is used to declare and Object is an instance of class.
create objects.
No memory is allocated when Memory is allocated as soon as
a class is declared. an object is created.
A class is a group of similar Object is a real-world
objects. entity such as book, car, etc.
Class is a logical entity. Object is a physical entity.
Class can only be Object can be created many
declared once. times as per requirement.
Objects of the class car can be
Example of class can be car.
BMW, Mercedes, jaguar, etc.

5.Predict output of the following snippet:


static int[] nums;
public static void main(String args[])
{
System.out.println(nums.length);
}
Ans: 0

6. Find the output of the snippet


Class A
{
private String a;
}
Class Main
{
public static void main(String[] args)
{
A obj=new A();
obj.a=”Hello”;
System.out.println(obj.a);
}
}
Ans:Hello

7.What will be stored in the object obj in the following line


of code? ItemType obj;
In the line of code ItemType obj;, an object reference
variable named obj is declared, but it is not initialized. In
Java, when you declare a reference variable without
initializing it, the variable contains null by default.

So, the obj variable will initially contain null. It doesn't


point to any object or instance of the ItemType class until
you explicitly assign an object to it using the new keyword or
some other method of object instantiation.

8. Find the output of the snippet


class simple
{
double x;
double y;
double z;
public static void main(String args[])
{
simple s1=new simple();
s1.x=5.0;
s1.y=6.0;
s1.z=7.0;
System.out.print(&quot;x=&quot;+x+&quot;;y=&quot;
+y+&quot;;z=&quot;+z);

}
ANS: x=5.0;y=6.0;z=7.0

9. class A
{
int i;
}
class Main
{

(CO1, K1)
public static void main(String args[])
{
A a = new A();
System.out.println(a.i);
}
}
ANS:0

10. Write a program to generate Fibonacci series 1 1 2 3 5 8 13 21 34


55 8
package sides;
import java.util.*;
public class Fibo {

public static void fibo(int n) {


if(n==1)
{
System.out.println(0);
}
else if(n==2)
{
System.out.println(0+" "+1);
}
else {
int n1=0;
int n2=1;
int n3=n1+n2;
System.out.println(0+"\n"+1);
for(int i=2;i<=n;i++)
{
System.out.println(n3);
n1=n2;
n2=n3;
n3=n1+n2;

}
}
}

public static void main(String[] args) {

int num;
Scanner scan=new Scanner(System.in);
num=scan.nextInt();
fibo(num);
}

PART B
1. Write a Java program to find area and perimeter of a circle
using class
public class Circle {
int r;
float area;
float perimeter;

public Circle(int r) {
this.r=r;
}
public void calArea() {
area=(float)(3.14*Math.pow(r, 2));
}
public void calPerimeter() {
perimeter=(float)(2*3.14*r);
}
public void display() {
System.out.println(area+" "+perimeter);
}
public static void main(String args[])
{
Circle c1=new Circle(7);
c1.calArea();
c1.calPerimeter();
c1.display();
}

2. Write a program that takes a list of numbers as input from


the user and calculates their sum and average
import java.util.*;

public class Avgandsum {


int Num;
int Value[];
int sum=0;
int avg;

public Avgandsum(int num,int value[]) {


this.Num=num;
this.Value=value;
}
public void calsum()
{
for(int i=0;i<Num;i++)
{
sum+=Value[i];
}
}
public void calavg()
{
avg=sum/Num;

}
public void display() {
System.out.println("the sum is "+sum+" avg is "+avg);
}

public static void main(String[] args) {

Scanner scan= new Scanner(System.in);


System.out.println("Enter your number of elements:");
int num=scan.nextInt();
int value[]=new int[num];
for(int i=0;i<num;i++)
{
System.out.println("enter your vale "+(i+1));
value[i]=scan.nextInt();
}
Avgandsum member1=new Avgandsum(num,value);
member1.calsum();
member1.calavg();
member1.display();
}

3. Write a Java program to read three subject marks and find


the average and print it using class

You might also like