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

Java_Project_File

Uploaded by

malik cp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Java_Project_File

Uploaded by

malik cp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Q1 Java Program to Read The Number From Standard Input

Ans1-> public class ReadNumberExample1


{
public static void main(String[] args)
{
//object of the Scanner class
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
//invoking nextInt() method that reads an integer input by keyboard
//storing the input number in a variable num
int num = sc.nextInt();
//closing the Scanner after use
sc.close();
//prints the number
System.out.println("The number entered by the user is: "+num);
}
}

Q2 Write a program that print your name on the screen.

Ans2->
public class PrintName {
public static void main(String[] args)
{
String name = "Your Name";
System.out.println("Hello, " + name + "!");
}
}

Q3 Write a program that fine the size of int, float, double and char.

Ans-> import java.util.*;

/* how to find size of variable in java */

class SizeofdataTypes
{
public static void main(String[] args) {
System.out.println("Size of int: " + (Integer.SIZE / 8) + " bytes.");
System.out.println("Size of long: " + (Long.SIZE / 8) + " bytes.");
System.out.println("Size of char: " + (Character.SIZE / 8) + " bytes.");
System.out.println("Size of float: " + (Float.SIZE / 8) + " bytes.");
System.out.println("Size of double: " + (Double.SIZE / 8) + " bytes.");
}
}
Q4 Write a program that check whether character is vowel or consonant.

Ans-> import java.io.*;


class Prep {
static void Vowel_Or_Consonant(char y)
{
if (y == 'a' || y == 'e' || y == 'i' || y == 'o'
|| y == 'u' || y == 'A' || y == 'E' || y == 'I'
|| y == 'O' || y == 'U')
System.out.println("It is a Vowel.");
else
System.out.println("It is a Consonant.");
}
static public void main(String[] args)
{
Vowel_Or_Consonant('W');
Vowel_Or_Consonant('I');
}
}

Q5 Write a program that find the larger number among three number.

Ans-> import java.io.*;

class PrepBytes {

// Function to find the biggest of three numbers


static int biggestOfThree(int x, int y, int z)
{

return z > (x > y ? x : y) ? z : ((x > y) ? x : y);


}

// Main driver function


public static void main(String[] args)
{

// Declaring variables for 3 numbers


int a, b, c;

// Variable holding the largest number


int largest;
a = 5;
b = 10;
c = 3;
// Calling the above function in main
largest = biggestOfThree(a, b, c);
// Printing the largest number
System.out.println(largest
+ " is the largest number.");
}
}

Q6 Write a program that fine the ASCII value of Character.

Ans-> public class AsciiValue


{
public static void main(String[] args)
{

char ch = 'a';
int ascii = ch;
// You can also cast char to int
int castAscii = (int) ch;

System.out.println("The ASCII value of " + ch + " is: " + ascii);


System.out.println("The ASCII value of " + ch + " is: " + castAscii);
}
}

Q7 Write a program that check Whether a number is palindrome or not.

Ans-> import java.util.Scanner;


public class Palindrome
{
public static void main(String args[])
{
int n, m, rev = 0, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
n = s.nextInt();
m = n;
while(n > 0)
{
x = n % 10;
rev = rev * 10 + x;
n = n / 10;
}
if(rev == m)
{
System.out.println(" "+m+" is a palindrome number");
}
else
{
System.out.println(" "+m+" is not a palindrome number");
}
}
}

Q8 Write a program that display the factor of a number.

Ans-> public class Main


{
public static void main(String[] args) {

int num = 10;

System.out.println( "Factors of " + num + " are " );

// finding and printing factors b/w 1 to num


for(int i = 1; i <= num; i++)
{
if(num % i == 0)
System.out.println(i + " ");
}

}
}
// Time Complexity : O(n)
// Auxiliary Space : O(1)

Q9 Write a program that create star pyramid on the screen.

Ans-> import java.io.*;

// Java code to demonstrate star patterns

public class GeeksForGeeks


{

// Function to demonstrate printing pattern

public static void printStars(int n)

int i, j;

// outer loop to handle number of rows

// n in this case

for(i=0; i<n; i++)


{

// inner loop to handle number of columns

// values changing acc. to outer loop

for(j=0; j<=i; j++)

// printing stars

System.out.print("* ");

// ending line after each row

System.out.println();

// Driver Function

public static void main(String args[])

int n = 5;

printStars(n);

}
}

Q10 Write a program that fine the area of rectangle and triangle by using the two different
function.

Ans-> class Rectangle implements Area


{
public double Compute(double l, double b)
{
return (l*b);
}
}

class Triangle implements Area


{
public double Compute(double b, double h)
{
return (b*h/2);
}
}

public class MainArea


{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
double RArea = rect.Compute(10, 20);
System.out.println("The area of the Rectangle is "+RArea);

Triangle tri = new Triangle();


double TArea = tri.Compute(10, 20);
System.out.println("The area of the triangle is "+TArea);

}
}

Q11 Write a program that make a calculator by using the function.

Ans-> import java.util.Scanner;

public class SimpleCalculator {


public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

System.out.print("Enter the first number: ");


int firstNumber = sc.nextInt();
System.out.print("Enter the second number: ");
int secondNumber = sc.nextInt();

System.out.print("Enter the type of operation you want to perform (+, -, *, /, %): ");
String operation = sc.next();
int result = performOperation(firstNumber, secondNumber, operation);
System.out.println("Your answer is: " + result);
}

public static int performOperation(int firstNumber, int secondNumber, String operation)


{
int result = 0;
if (operation.equals("+")) {
result = firstNumber + secondNumber;
}
else if (operation.equals("-")) {
result = firstNumber - secondNumber;
}
else if (operation.equals("*")) {
result = firstNumber * secondNumber;
}
else if (operation.equals("%")) {
result = firstNumber % secondNumber;
}
else if (operation.equals("/")) {
result = firstNumber / secondNumber;
}
else {
System.out.println("Invalid operation");
}
return result;
}
}

Q12 Write a program that create the class and object.

Ans-> / Java Program for class example

class Student {

// data member (also instance variable)

int id;

// data member (also instance variable)

String name;

public static void main(String args[])

// creating an object of

// Student

Student s1 = new Student();

System.out.println(s1.id);
System.out.println(s1.name);

}
}

Q13 Write a program that take the student detail and display on the screen by the help of class.

Ans-> //program to get student details


import java.util.Scanner;

public class GetStudentDetails


{
public static void main(String args[])
{
String name;
int roll, math, phy, eng;

Scanner SC=new Scanner(System.in);

System.out.print("Enter Name: ");


name=SC.nextLine();
System.out.print("Enter Roll Number: ");
roll=SC.nextInt();
System.out.print("Enter marks in Maths, Physics and English: ");
math=SC.nextInt();
phy=SC.nextInt();
eng=SC.nextInt();

int total=math+eng+phy;
float perc=(float)total/300*100;

System.out.println("Roll Number:" + roll +"\tName: "+name);


System.out.println("Marks (Maths, Physics, English): " +math+","+phy+","+eng);
System.out.println("Total: "+total +"\tPercentage: "+perc);

}
Q14 Write a program that show the working of Multilevl inheritance.

Ans->class Animal {

// field and method of the parent class


String name;
public void eat() {
System.out.println("I can eat");
}
}

// inherit from Animal


class Dog extends Animal {

// new method in subclass


public void display() {
System.out.println("My name is " + name);
}
}

class Main {
public static void main(String[] args) {

// create an object of the subclass


Dog labrador = new Dog();

// access field of superclass


labrador.name = "Rohu";
labrador.display();

// call method of superclass


// using object of subclass
labrador.eat();

}
}

Q15 Write a program that create the package.

Ans-> package p2;


import java.util.*;
public class Sub
{
int d;
public void diff()
{
System.out.print("Enter the first number: ");
Scanner scan=new Scanner(System.in);
int x=scan.nextInt();
System.out.print("Enter the second number: ");
Scanner scan1=new Scanner(System.in);
int y=scan1.nextInt();
d=x-y;
System.out.println("Difference="+d);
}
}

Q16 Write a program that show the working of JDBC.

Ans-> import java.io.*;

import java.sql.*;

class GFG {

public static void main(String[] args) throws Exception

String url

= "jdbc:mysql://localhost:3306/table_name"; // table details

String username = "rootgfg"; // MySQL credentials

String password = "gfg123";

String query

= "select *from students"; // query to be run

Class.forName(

"com.mysql.cj.jdbc.Driver"); // Driver name

Connection con = DriverManager.getConnection(

url, username, password);

System.out.println(

"Connection Established successfully");

Statement st = con.createStatement();
ResultSet rs

= st.executeQuery(query); // Execute query

rs.next();

String name

= rs.getString("name"); // Retrieve name from db

System.out.println(name); // Print result on console

st.close(); // close statement

con.close(); // close connection

System.out.println("Connection Closed....");

}
}

Q17 Write a program that function over loading. Create minimum same name 4 function.

Ans-> // Java program to demonstrate working of method


// overloading in Java

public class Sum {

// Overloaded sum(). This sum takes two int parameters

public int sum(int x, int y) { return (x + y); }

// Overloaded sum(). This sum takes three int parameters

public int sum(int x, int y, int z)

return (x + y + z);

// Overloaded sum(). This sum takes two double


// parameters

public double sum(double x, double y)

return (x + y);

// Driver code

public static void main(String args[])

Sum s = new Sum();

System.out.println(s.sum(10, 20));

System.out.println(s.sum(10, 20, 30));

System.out.println(s.sum(10.5, 20.5));

}
}

Q18 Write a program that show the working of function hiding.

Ans->// Java program to demonstrate


// method Hiding in java

// Base Class

class Complex {

public static void f1()

System.out.println(

"f1 method of the Complex class is executed.");

}
}
// class child extend Demo class

class Sample extends Complex {

public static void f1()

System.out.println(

"f1 of the Sample class is executed.");

}
}

public class Main {

public static void main(String args[])

Complex d1 = new Complex();

// d2 is reference variable of class Demo that

// points to object of class Sample

Complex d2 = new Sample();

// But here method will be call using type of

// reference

d1.f1();

d2.f1();

}
}
Q19 Write a program for Interface.

Ans-> interface printable{


void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}

public static void main(String args[]){


A6 obj = new A6();
obj.print();
}
}

Q20 Write a program show the working of abstract class.

Ans->

// Java Program to implement Abstract Class


// having constructor, data member, and methods

import java.io.*;

abstract class Subject {

Subject() {

System.out.println("Learning Subject");

abstract void syllabus();

void Learn(){

System.out.println("Preparing Right Now!");

}
}

class IT extends Subject {

void syllabus(){
System.out.println("C , Java , C++");

}
}

class GFG {

public static void main(String[] args) {

Subject x=new IT();

x.syllabus();

x.Learn();

}
}

Q21 Write a program show the working of constructor and destructor.

Ans-> public class DestructorExample


{
public static void main(String[] args)
{
DestructorExample de = new DestructorExample ();
de.finalize();
de = null;
System.gc();
System.out.println("Inside the main() method");
}
protected void finalize()
{
System.out.println("Object is destroyed by the Garbage Collector");
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy