Skip to document

All Java Programs - fdsafgdfg trhthtr hdFf

fdsafgdfg trhthtr hdFf
Course

Software Engineering (CS530)

376 Documents
Students shared 376 documents in this course
Academic year: 2022/2023
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
Visvesvaraya Technological University

Comments

Please sign in or register to post comments.

Preview text

Program 1:

import static java.lang.Math; import java.util; public class Quadratic { public static void main(String[] args) { // TODO code application logic here Scanner sc=new Scanner(System); double a,b,c; //Quadratic Variables declaration System.out("Enter the value of a.."); a=sc(); //Quadratic Variables Initialization System.out("Enter the value of b.."); b=sc(); //Quadratic Variables Initialization System.out("Enter the value of c.."); c=sc(); //Quadratic Variables Initialization if(a==0 || (a==0 && b==0)) { System.out("Roots cant'b computed"); } else { double det=(bb)-(4a*c); //Find the determinant

double r=2*a;

//Check for Roots

if(det>0) { System.out("Roots are real and unequal"); double root1 = (-b + Math((det))) / (2 * a); double root2 = (-b - Math((det))) / (2 * a); System.out("Roots are.."); System.out("Root1:"+root1); System.out("Root2:"+root2); } else if(det==0) { System.out("The roots of the quadratic equation are real and equal."); double root=(-b)/r; System.out("Root:"+root); } else { System.out("The roots of the quadratic equation are complex and different"); double realPart = -b / (2 * a); double imagPart = Math(abs(det)) / (2 * a); System.out("Roots are "); System.out(realPart+" +i" + imagPart); System.out(realPart+" -i" + imagPart); } }

System.out("Phone No of the Student :"+ phoneNo); } } public class StudentDemo /* class which contains main method / { public static void main(String [] args) { int nStudent,ch; / create new instance of Scanner class to read the input from console / Scanner in = new Scanner(System); System.out("Enter the no of student objects to be created"); nStudent = in(); / Declare array of student type */ Student stud [] = new Student [nStudent]; while(true) { System.out("1:Create Student Data"); System.out("2:Print Student Data"); System.out("3:Exit"); ch = in();

switch(ch) { case 1 :

for(int i=0;i<nStudent;i++) { System.out("Read the Student" +" "+(i+1)+" Details");

in(); System.out("Enter the name of the student"); String name = in(); System.out("Enter the usn of the student"); String usn = in(); System.out("Enter the branch of the student"); String branch = in(); System.out("Enter the phoneNo of the student"); long phoneNo = in(); /* create the object of student type */ stud[i] = new Student(usn,name,branch,phoneNo);

} break; case 2 : for(int i=0;i<nStudent;i++) { System.out("Details of Student"+" "+(i+1)+" is \n");

stud[i].printStudentData();

count++; } } if(count>2) { System.out("The number"+' '+num+' '+"is not a prime number"); } else { System.out("The number"+' '+num+' '+"is a prime number"); }

}

} Calculator: import java.; import java.; import java.lang; import java.util; public class BasicCalculator { public static void main(String[] args)

{

// stores two numbers double num1, num2; // Take input from the user Scanner sc = new Scanner(System); System.out("Enter the numbers"); // take the inputs num1 = sc(); num2 = sc(); System.out("Enter the operator (+,-,*,/)");char op = sc().charAt(0); double o = 0; switch (op) { // case to add two numberscase '+': o = num1 + num2; break; // case to subtract two numberscase '-': o = num1 - num2; break;

// print the final result System.out(num1 + " " + op + " " + num

  • " = " + o); } }

Program 4:

import java.util;

class Staff {

int staffId; String name; long phoneNo;int salary; /* Scanner class to read from console / Scanner in = new Scanner(System); / Method to read the Staff data */void accept() {

System.out("Enter the name of the staff");name = in(); System.out("Enter the staffId"); staffId = in(); System.out("Enter the phoneNo");phoneNo = in(); System.out("Enter the salary"); salary = in(); } /* Method to display the Staff data */

void display() { System.out("--------------------------------- "); System.out("StaffId:" + " " + staffId); System.out("Name: " + " " + name); System.out("PhoneNo: " + " " + phoneNo); System.out("Salary: " + " " + salary); } } /* Inheritance of Staff class by StaffTeaching class */class StaffTeaching extends Staff

System.out("Domain: " + domain);

System.out("Publications:");

for (int i = 0; i < publications; i++) {

System.out(publications[i]);

}

}

}

/*Subclass 2 */ class StaffTechnical extends Staff { private String[] skills; public void accept() { super(); Scanner scanner = new Scanner(System); System.out("Enter Number of Skills: "); int n = scanner(); skills = new String[n]; System.out("Enter Skills:"); for (int i = 0; i < n; i++) { skills[i] = scanner(); } }

public void display() { super(); System.out("Skills:"); for (int i = 0; i < skills; i++) { System.out(skills[i]); }

}

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

//Declare an array of type StaffTeaching,StaffTechnical and StaffContract

StaffTeaching stt[] = new StaffTeaching[3]; StaffTechnical stec[] = new StaffTechnical[3]; StaffContract stc[] = new StaffContract[3]; System.out("Read Teaching Staff Details");for(int i=0;i<3;i++) { stt[i] = new StaffTeaching(); System.out("Enter the Teaching Staff " + (i+1)+ " details");

stt[i].accept(); } System.out("Read Technical Staff Details");for(int i=0;i<3;i++) { stec[i] = new StaffTechnical(); System.out("Enter the Technical Staff " + (i+1)+ " details"); stec[i].accept(); } System.out("Read Contract Staff Details"); for(int i=0;i<3;i++) {

stc[i] = new StaffContract(); System.out("Enter the Contract Staff " + (i+1)+ " details"); stc[i].accept(); }

for(int i=0;i<3;i++) { System.out("Details of Teaching Staff "+(i+1) +" is:" );stt[i].display(); }

for(int i=0;i<3;i++) { System.out("Details of Technical Staff " +" "+ (i+1) + " is:" );stec[i].display(); }

for(int i=0;i<3;i++) { System.out("Details of Contract Staff " +" "+ (i+1) + " is:" ); stc[i].display(); } } }

Program 5:

marks3=m3;

}

// copy constructor

student(student s)

{

name=s;

regno=s;

marks1=s;

marks2=s;

marks3=s;

}

void display()

{

System.out(name + "\t" +regno+ "\t" +marks1+ "\t"

+marks2+ "\t" + marks3+"\n");

}

}

class HelloWorld {

public static void main(String[] args) {

student s1=new student();

student s2=new student("john",34266,58,96,84);

student s3=new student(s1);

s1();

s2();

s3();

}

}

Method Overloading:

class DisplayOverloading

{

//adding two integer numbers

int add(int a, int b)

{

int sum = a+b;

return sum;

}

//adding three integer numbers

int add(int a, int b, int c)

{

int sum = a+b+c;

return sum;

}

float add(float a,float b)

Was this document helpful?

All Java Programs - fdsafgdfg trhthtr hdFf

Course: Software Engineering (CS530)

376 Documents
Students shared 376 documents in this course
Was this document helpful?
Program 1:
import static java.lang.Math.abs;
import java.util.Scanner;
public class Quadratic {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc=new Scanner(System.in);
double a,b,c; //Quadratic Variables declaration
System.out.println("Enter the value of a..");
a=sc.nextDouble(); //Quadratic Variables Initialization
System.out.println("Enter the value of b..");
b=sc.nextDouble(); //Quadratic Variables Initialization
System.out.println("Enter the value of c..");
c=sc.nextDouble(); //Quadratic Variables Initialization
if(a==0 || (a==0 && b==0))
{
System.out.println("Roots cant'b computed");
}
else
{
double det=(b*b)-(4*a*c); //Find the determinant
double r=2*a;
//Check for Roots