Java Programming Part A
Java Programming Part A
: INDEX – PART A :
1. Java program to assign two integer values to X and Y. Using the ‘if’ statement the
output of the program should display a message whether X is greater than Y.
import java.util.*;
public class Comparision
{
public static void main(String[] args)
{
int x,y;
System.out.println("Enter two operands");
Scanner sc = new Scanner(System.in);
x= sc.nextInt();
y= sc.nextInt();
if(x>y)
{
System.out.println(x + " is greater");
}
else
{
System.out.println(y + " is greater");
}
}
}
OUTPUT:
2. Java program to list the factorial of the numbers 1 to 10. To calculate the factorial
value, use while loop. (Hint: Fact of 4 = 4*3*2*1)
import java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{
int fact = 1;
int i = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number whose factorial is to be found: ");
int num = sc.nextInt();
while( i <= num )
{
fact = fact * i;
i++;
}
System.out.println("\n Factorial of " + num + " is: " + fact);
}
}
OUTPUT:
import java.util.Scanner;
public class Circle123
{
public static void main(String[] args)
{
double radius, area, circum;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Radius of Circle: ");
radius = s.nextFloat();
area = Math.PI*radius*radius;
System.out.println("\nArea = " +area);
circum= Math.PI * 2*radius;
System.out.printf( "\n Circumference is: %.2f",circum) ;
}
}
OUTPUT:
OUTPUT:
class addsub
{
int num1;
int num2;
addsub(int n1, int n2)
{
num1 = n1;
num2 = n2;
}
int add()
{
return num1+num2;
}
int sub()
{
return num1-num2;
}
{
return num2%num1;
}
OUTPUT:
6. Program with class variable that is available for all instances of a class. Use static
variable declaration. Observe the changes that occur in the object’s member
variable values.
class Student
{
int rollno;//instance variable
String name;
static String college = " GFGC HOSADURGA ";//static variable
//constructor
Student(int r, String n)
{
rollno = r;
name = n;
}
//method to display the values
void display ()
{
System.out.println( "\t Roll No student is = " +rollno+ "\n \t Name of
student is = " +name+ " \n \t Name of college is =" +college);
}
}
//Test class to show the values of objects
public class Static_variable
{
public static void main(String args[])
{
Student s1 = new Student(111101,"NANDAN");
Student s2 = new Student(111102,"ARYAN");
//we can change the college of all objects by the single line of code \n
//Student.college="GFGC";
s1.display();
s2.display();
}
}
OUTPUT:
import java.util.Scanner;
public class Student_details
{
public static void main(String[] args)
{
Student s1 = new Student("1001","CHETAN",91,89,79);
s1.printStudentDetails();
Student s2 = new Student();
s2.getStudentDetails();
s2.printStudentDetails();
}
}
class Student
{
String usn;
String name;
int marks1,marks2,marks3;
int totalMarks;
Student()
{
}
Student(String usn,String name,int marks1,int marks2,int marks3)
{
this.usn = usn;
this.name = name;
this.marks1 = marks1;
this.marks2 = marks2;
this.marks3 = marks3;
if(marks1 >= 50 & marks2 >= 50 & marks3 >= 50)
totalMarks = marks1 + marks2 + marks3;
else totalMarks =0;
}
void getStudentDetails()
{
System.out.println("Enter usn,name,marks of three subjects");
Scanner sc = new Scanner(System.in);
usn = sc.next();
name = sc.next();
marks1 = sc.nextInt();
marks2 = sc.nextInt();
marks3 = sc.nextInt();
if(marks1 >= 50 & marks2 >= 50 & marks3 >= 50)
totalMarks = marks1 + marks2 + marks3;
else totalMarks =0;
}
void printStudentDetails()
{
System.out.println("USN: " +this.usn);
System.out.println("Name: " +this.name);
System.out.println("Sub1 marks: " +this.marks1);
System.out.println("Sub2 marks: " +this.marks2);
System.out.println("Sub3 marks: " +this.marks3);
System.out.println("Total marks: " +this.totalMarks);
}
}
OUTPUT:
// Driver Code
class Car_attributes
{
public static void main(String args[])
{
// Creating a new object of the Hybrid Car class
Hybrid_Car obj = new Hybrid_Car();
// Calling the methods of the Hybrid_Car class
obj.drive();
obj.cng_kit();
obj.petrol_kit();
}
}
OUTPUT:
OUTPUT: