0% found this document useful (0 votes)
29 views11 pages

Java Me-1to 3

Uploaded by

Nav Sood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views11 pages

Java Me-1to 3

Uploaded by

Nav Sood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

NAME:-Ravi Raj Safi

CLASS:-CS2
ROLL_Number:-1800293

MACHINE EXERCISE:-01
1. Write a Java program that prints all real solutions to the quadratic
equation ax2 + bx+ c=0. Read in a, b, c and use the quadratic formula.
If the discriminant b2 –4ac is negative, display a message stating that
there are no real solutions.
//Program to find roots of a quadratic equations
import java.util.*;
class Roots
{
public static void main(String args[])
{
int a,b,c,d,f=0;
Scanner scr=new Scanner(System.in);
System.out.println("\nEnter the values of a ,b ,c : ");
a=scr.nextInt();
b=scr.nextInt();
c=scr.nextInt();
d=(b*b)-(4*a*c);
if(d==0)
{
System.out.println("Roots are real and Equal");
f=1;
}
else if(d>0)
{
System.out.println("Roots are real and UnEqual");
f=1;
}
else
System.out.println("Roots are imaginary");
if(f==1)
{
float r1=(float)(-b+Math.sqrt(d))/(2*a);
float r2=(float)(-b-Math.sqrt(d))/(2*a);
System.out.println("Roots are : "+r1+" ,"+r2);
}
}
Output:

Enter the values of a ,b ,c :


1
2
-3
Roots are real and UnEqual
Roots are : 1.0 ,-3.0

2. The Fibonacci sequence is defined by the following rule. The first


two values in the sequence are 1 and 1. Every subsequent values is
the sum of the two values preceding it. Write a java program that uses
both recursive and non recursive functions to print the nth value in
the fibonacci sequence.
//Program to print nth value in the fibonacci series
import java.util.*;
class Function
{
void nrcf(int a,int b,int c,int n)
{
for(int i=1;i<=n-2;i++)
{
c=a+b;
a=b;
b=c;
}
a=b=1;
System.out.println("nth value in the series using non recursive function is : "+c);

}
void rcf(int a,int b,int c,int n)
{

if(n-2>0)
{
c=a+b;
a=b;
b=c;
rcf(a,b,c,n-1);
return;
}
System.out.println("\nnth value in the series using recursive function is : "+c);
}
}

class Fibonacci
{
public static void main(String args[])
{
Function f=new Function();
int n,a=1,b=1,c=0;
Scanner scr=new Scanner(System.in);
System.out.println("\nEnter n value: ");
n=scr.nextInt();
f.nrcf(a,b,c,n);
f.rcf(a,b,c,n);

}
}

Output:

Enter n value:
10
nth value in the series using non recursive function is : 55
nth value in the series using recursive function is : 55

3. Write a Java program that prompts the user for an integer and then
prints out all prime numbers up to that integer.

//Program to print prime numbers upto given integer


import java.util.*;
class Prime
{
public static void main(String args[])
{
int n,f;
Scanner scr=new Scanner(System.in);
System.out.println("\nEnter n value: ");
n=scr.nextInt();
System.out.println("\nPrimenumbers are : ");
for(int i=2;i<=n;i++)
{
f=0;
for(int j=2;j<=i/2;j++)
if((i%j)==0)
{
f=1;
break;
}
if(f==0)
System.out.print(i+" ");
}
}
}

Output:

Enter n value:
10
Prime numbers are :
2 3 5 7

4. Write a Java program that prints the following pattern

public class Pattern {

public static void main(String[] args) {


int rows = 6;

for(int i = rows; i >= 1; --i) {


for(int j = 1; j <= i; ++j) {
System.out.print("X ");
}
System.out.println();
}
}
}

Output:

xxxxxx
xxxxx
xxxx
xxx
xx
x
5. Write a Java program that calculate mathematical constant ‘e’ using
the formula e=1+1/2!+1/3!+........ up to 5
// A simple Java program to compute
// sum of series 1/1! + 1/2! + .. + 1/n!
import java.io.*;

class SOS {

// Utility function to find


static int factorial(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res *= i;
return res;
}

// A Simple Function to return value


// of 1/1! + 1/2! + .. + 1/n!
static double sum(int n)
{
double sum = 0;
for (int i = 1; i <= n; i++)
sum += 1.0/factorial(i);
return sum;
}

// Driver program
public static void main (String[] args)
{
int n = 5;
System.out.println(sum(n));
}
}

Output:
1.71667

MACHINE EXERCISE:-02

1. Write a Java program that reverses a given String


// Java program to demonstrate conversion from
// String to StringBuffer and reverse of string
import java.lang.*;
import java.io.*;
import java.util.*;

public class Test {


public static void main(String[] args)
{
String str = "JAVA";

// conversion from String object to StringBuffer


StringBuffer sbr = new StringBuffer(str);
// To reverse the string
sbr.reverse();
System.out.println(sbr);
}
}

Output-
AVAJ

2. Write a Java program that checks whether a given string is a


palindrome or not.

import java.util.Scanner;

class ChkPalindrome
{
public static void main(String args[])
{
String str, rev = "";
Scanner sc = new Scanner(System.in);

System.out.println("Enter a string:");
str = sc.nextLine();

int length = str.length();

for ( int i = length - 1; i >= 0; i-- )


rev = rev + str.charAt(i);

if (str.equals(rev))
System.out.println(str+" is a palindrome");
else
System.out.println(str+" is not a palindrome");

}
}

Output-
Enter a string:
radar

radar is a palindrome

3. Write a Java program to count the frequency of words, characters


in the given line of text

// Java program to count the


// number of charaters in a file
import java.io.*;

public class Test


{
public static void main(String[] args) throws IOException
{
File file = new File("C:\\Users\\Mayank\\Desktop\\1.txt");
FileInputStream fileStream = new FileInputStream(file);
InputStreamReader input = new InputStreamReader(fileStream);
BufferedReader reader = new BufferedReader(input);

String line;

// Initializing counters
int countWord = 0;
int sentenceCount = 0;
int characterCount = 0;
int paragraphCount = 1;
int whitespaceCount = 0;

// Reading line by line from the


// file until a null is returned
while((line = reader.readLine()) != null)
{
if(line.equals(""))
{
paragraphCount++;
} else {
characterCount += line.length();

// \\s+ is the space delimiter in java


String[] wordList = line.split("\\s+");

countWord += wordList.length;
whitespaceCount += countWord -1;
// [!?.:]+ is the sentence delimiter in java
String[] sentenceList = line.split("[!?.:]+");

sentenceCount += sentenceList.length;
}
}

System.out.println("Total word count = " + countWord);


System.out.println("Total number of sentences = " + sentenceCount);
System.out.println("Total number of characters = " + characterCount);
System.out.println("Number of paragraphs = " + paragraphCount);
System.out.println("Total number of whitespaces = " + whitespaceCount);
}
}

Output-

Total word count = 5


Total number of sentences = 3
Total number of characters = 21
Number of paragraphs = 2
Total number of whitespaces = 7

4. Write a Java program for sorting a given list of names in ascending


order.
import java.util.*;
class Sorting
{
void sortStrings()
{
Scanner s = new Scanner(System.in);
System.out.println("Enter the value of n: ");
int n = s.nextInt();
String[] str = new String[n];
System.out.println("Enter strings: ");
for(int i = 0; i < n; i++)
{
str[i] = new String(s.next());
}
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
if(str[i].compareTo(str[j])>0)
{
String temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
System.out.println("Sorted list of strings is:");
for(int i = 0; i < n ; i++)
{
System.out.println(str[i]);
}
}
}
class Driver
{
public static void main(String[] args)
{
Sorting obj = new Sorting();
obj.sortStrings();
}
}

Output-

Enter the value of n:


4
Enter strings:
suresh
mahesh
dinesh
ganesh
Sorted list of strings is:
dinesh
ganesh
mahesh
suresh

MACHINE EXERCISE:-03

1.Write a Java program that displays area of different


Figures(Rectangle,Square,Triangle) using the method overloading
class AreaShape {
void area(int x) {
System.out.println(“Area of square is = “ + (x*x));
}
void area(int x,int y) {
System.out.println(“Area. of rectangle is = “ + (x*y))
}
void area(int x,int y,int z) {
doub1e s=(x+y+z)/2;
double triArea;
triArea = Math.sqrt(s*(s–x)*(s–y)*(s–z));
System.out.println(“Area of triangle is = “ + triArea);
}
}
public class AreaOverload {
public static void main(String[] args) {
AreaShape obj = new AreaShape();
obj.area(10); //Call area(int x)
obj.area(5,6); //Call area(int x,int y)
obj.area(4,5,6); //Call area(int x,int y,int z)
}
}

Output :

Area of square is = 100


Area of rectangle is = 30
Area of triangle is = 6.48074069840786

2. Write a Java program that displays that displays the time in


different formats in the form of HH,MM,SS using constructor
Overloading

3. Write a Java program that counts the number of objects created by


using static variable

// Java program Find Out the Number of Objects Created


// of a Class
class Test {

static int noOfObjects = 0;

// Instead of performing increment in the constructor


// instance block is preferred to make this program generic.
{
noOfObjects += 1;
}
// various types of constructors
// that can create objects
public Test()
{
}
public Test(int n)
{
}
public Test(String s)
{
}

public static void main(String args[])


{
Test t1 = new Test();
Test t2 = new Test(5);
Test t3 = new Test("GFG");

// We can also write t1.noOfObjects or


// t2.noOfObjects or t3.noOfObjects
System.out.println(Test.noOfObjects);
}
}

Output-
3

4. Write a java program that implements educational hierarchy using


inheritance.

You might also like