Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

G.

NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

1. Write a Java program that prints all real solutions to the quadratic equation
ax 2 + bx + c=0. Read in a, b, c and use the quadratic formula. If the discriminant
b 2 -4ac is negative, display a message stating that there are no real solutions.
Program:
package myfirstproject;
import java.util.*;
import java.lang.Math;
public class Quadratic {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter a,b,c values in ax2+bx+c=0 :");
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
double d=(b*b)-(4*a*c);
double l,r,f;
f=2*a;
if(d<0) {
System.out.println("There are no real solutions!");
}
else if(d>0) {
l= (-b-(Math.sqrt(d)))/f;
r=(-b+(Math.sqrt(d)))/f;

22251A05B3 II BTECH II SEMESTER 2023-2024


G. NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

System.out.println("The roots are two roots:"+l+" "+r);


}
else {
double z=-(b*b)/(4*a);
double w=-1*z;
System.out.println("The roots are equal:"+z+" "+w);
}
}

Output:
1.
Enter a,b,c values in ax2+bx+c=0 :
1 -2 1
The roots are equal: -1.0 1.0
2.
Enter a,b,c values in ax2+bx+c=0 :
1 -7 10
The roots are two roots: 2.0 5.0
3.
Enter a,b,c values in ax2+bx+c=0 :
112
There are no real solutions!

22251A05B3 II BTECH II SEMESTER 2023-2024


G. NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

2. Write a Java program that checks whether the given string is a palindrome or not.
Program:
package myfirstproject;
import java.util.*;
public class StringPalindrome {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string:");
String s=sc.next();
int n=s.length();
String s1="";
for(int i=n-1;i>=0;i--) {
char ch=s.charAt(i);
s1=s1+ch;
}
if(s.equals(s1)) {
System.out.println("Palindrome");
}
else
System.out.println("Not a palindrome");
}
}

Output:
1.
Enter the string:
malayalam
Palindrome
2.
Enter the string:
right
Not a palindrome

22251A05B3 II BTECH II SEMESTER 2023-2024


G. NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

3. Write a Java program to multiply given 3X3 matrices.


Program:
package myfirstproject;
import java.util.*;
public class MultiplicationOfMatrices {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int m1[][]=new int[3][3];
int m2[][]=new int[3][3];
int m3[][]=new int[3][3];
int i,j;
System.out.println("Enter 1st matrix: ");
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
m1[i][j]=sc.nextInt();
}
}
System.out.println("Enter 2nd matrix: ");
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
m2[i][j]=sc.nextInt();
}
}
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
m3[i][j]=0;
for(int k=0;k<3;k++) {
m3[i][j]+=m1[i][k]*m2[k][j];
}
}
}

22251A05B3 II BTECH II SEMESTER 2023-2024


G. NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

System.out.println("Result matrix: ");


for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
System.out.print(m3[i][j]+" ");
}
System.out.println();
}
}
}

Output:
Enter 1st matrix:
123
456
789
Enter 2nd matrix:
123
345
456
Result matrix:
19 25 31
43 58 73
67 91 115

22251A05B3 II BTECH II SEMESTER 2023-2024


G. NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

4. Write a Java program that accepts a number from the end-user and then prints all
prime numbers up to a given number.
Program:
package myfirstproject;
import java.util.*;
public class PrimeNumbers {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter n value:");
int n=sc.nextInt();
int i,j;
System.out.println("Prime Numbers are :");
for(i=2;i<n;i++) {
int c=0;
for(j=1;j<=i;j++) {
if((i%j)==0)
c+=1;
}
if(c==2) {
System.out.print(i+" ");
}
}
}

OutPut :
Enter n value:
10
Prime Numbers are :
2357

22251A05B3 II BTECH II SEMESTER 2023-2024


G. NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

5. Write a Java program to create a Box class with properties like length, breadth,
width, and volume. Display the volume of 2 different boxes by using objects.
Program:
package myfirstproject;
import java.util.*;
class Box{
int length;
int breadth;
int width;
void volume() {
System.out.println("Volume :"+length*breadth*width);
}
}
public class BoxClass{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Box mybox1=new Box();
Box mybox2=new Box();
System.out.println("Enter first box dimensions: ");
mybox1.length=sc.nextInt();
mybox1.breadth=sc.nextInt();
mybox1.width=sc.nextInt();
mybox1.volume();
System.out.println("Enter second box dimensions: ");
mybox2.length=sc.nextInt();
mybox2.breadth=sc.nextInt();
mybox2.width=sc.nextInt();
mybox2.volume();
}
}
Output:
Enter first box dimensions:
345
Volume :60
Enter second box dimensions:
631
Volume :18

22251A05B3 II BTECH II SEMESTER 2023-2024


G. NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

6. Write a Java program that demonstrates constructor overloading.

Program:
package javaprogram;
import java.util.*;
class Vol{
int width=0,height=0,depth=0;
Vol(int w,int h, int d){
width=w;
height=h;
depth=d;
System.out.println("vol: "+(width*height*depth));
}
Vol(int l){
width=height=depth=l;
System.out.println("vol: "+(width*height*depth));
}
Vol(){
System.out.println("vol: "+(width*height*depth)); }
}
public class construct {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter w,h,d: ");
int w,h,d;
w=sc.nextInt();
d=sc.nextInt();
h=sc.nextInt();
Vol mybox1=new Vol(w,h,d);
Vol mybox2=new Vol(w);
Vol mybox3=new Vol(); }
}
Output:
Enter w,h,d:
351
vol: 15
vol: 27
vol: 0

22251A05B3 II BTECH II SEMESTER 2023-2024


G. NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

7. Write a Java program to implement the use of inner classes.


Program:
package myfirstproject;
import java.util.Scanner;
class out1 {
int x;
void test(int a) {
In i=new In();
i.dis(a);}
class In{
void dis(int a) {
x=a;
System.out.println(x); } } }
public class Inner {
public static void main(String [] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter n: ");
int n=sc.nextInt();
while(n--!=0) {
System.out.println("Enter t: ");
int t=sc.nextInt();
out1 o=new out1();
o.test(t);}}}

Output:
Enter n:
3
Enter t:
4
4
Enter t:
3
3
Enter t:
2
2

22251A05B3 II BTECH II SEMESTER 2023-2024


G. NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

8. Write a Java program that demonstrates method overloading.


Program:
package myfirstproject;
import java.util.*;
class Method {
int a,b;
public int sum(int x,int y) {
a=x;
b=y;
return (a+b);
}
public int sum(int x) {
a=b=x;
return(a+b);
}
public int sum() {
a=b=0;
return (a+b); } }
public class MethodOverloading {
public static void main(String[] args) {
// TODO Auto-generated method stub
Method m=new Method();
Scanner sc=new Scanner(System.in);
System.out.println("Enter a,b:");
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println(m.sum(a,b));
System.out.println(m.sum(a));
System.out.println(m.sum()); } }

Output:
Enter a,b:
10 20
30
20
0

22251A05B3 II BTECH II SEMESTER 2023-2024


G. NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

9. Write a Java program that demonstrates method overriding.


Program:
package myfirstproject;
class Child{
public void show() {
System.out.println("Child's show.");
}
}
class Parent extends Child{
public void show() {
System.out.println("Parent's show");
}
}
public class MethodOverRiding {

public static void main(String[] args) {

Parent p=new Parent();


Child c=new Child();
c.show();
p.show();
}

Output:
Child's show.
Parent's show

22251A05B3 II BTECH II SEMESTER 2023-2024


G. NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

10. Write a Java program to create an abstract class named ‘Shape’ that contains two
integers and an empty method named printArea( ). Provide three classes named
Rectangle, Triangle and Circle such that each one of the classes extends the class
Shape. Each one of the classes contains only the method printArea( ) that prints the
area of the given shape.
Program:
package myfirstproject;
abstract class Shape{
int a=50,b=30;
abstract void PrintArea();
}
class Rect extends Shape{
void PrintArea() {
System.out.println("Rectangle:"+a*b);
}
}
class tri extends Shape{
void PrintArea() {
System.out.println("Triangle:"+(0.5)*a*b);
}
}
class Cir extends Shape{
public void PrintArea() {
System.out.println("Circle:"+a*a*(3.14));
}
}
public class AbstractArea {
public static void main(String[] args) {
Rect r=new Rect();
r.PrintArea();
tri t=new tri();
t.PrintArea();
Cir c=new Cir();
c.PrintArea();
}
}

22251A05B3 II BTECH II SEMESTER 2023-2024


G. NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

Output:
Rectangle:1500
Triangle:750.0
Circle:7850.0

22251A05B3 II BTECH II SEMESTER 2023-2024


G. NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE (FOR WOMEN)

TITLE: DATE:
PAGE NO:

11. Write a Java program that implements multiple inheritance.


Program:
package myfirstproject;
import java.util.*;
interface I1{
void seta(int j);
}
interface I2{

void setb(int i);


}
class P implements I1,I2{
int a,b;
public void setb(int i) {
a=i;
}
public void seta(int j) {
b=j;
}
void print() {
System.out.println(“Sum :”+(b+a));
}
}
public class Function {
public static void main(String[] args) {
P ob=new P();
ob.seta(23);
ob.setb(9);
ob.print();
}
}

Output:
Sum: 32

22251A05B3 II BTECH II SEMESTER 2023-2024

You might also like