Академический Документы
Профессиональный Документы
Культура Документы
Java is related to C++, which is a direct descendent of C. Much of the character of Java is inherited from these two languages. From C, Java derives its syntax. Many of Javas object-oriented features were influenced by C++. In fact, several of Javas defining characteristics come from or are responses to its predecessors. Moreover, the creation of Java was deeply rooted in the process of refinement and adaptation that has been occurring in computer programming languages for the past three decades. For these reasons, this section reviews the sequence of events and forces that led up to Java. As you will see, each innovation in language design was driven by the need to solve a fundamental problem that the preceding languages could not solve. Java is no exception. Java can be used to create two types of programs: applications and applets. An application is a program that runs on your computer, under the operating system of that computer. That is, an application created by Java is more or less like one created using C or C++. When used to create applications, Java is not much different from any other computer language. Rather, it is Javas ability to create applets that makes it important. An applet is an application designed to be transmitted over the Internet and executed by a Java-compatible Web browser. An applet is actually a tiny Java program, dynamically downloaded across the network, just like an image, sound file, or video clip. The important difference is that an applet is an intelligent program, not just an animation or media file. In other words, an applet is a program that can react to user input and dynamically changenot just run the same animation or sound over and over. No discussion of the genesis of Java is complete without a look at the Java buzzwords. Although the fundamental forces that necessitated the invention of Java are portability and security, other factors also played an important role in molding the final form of the language. The key considerations were summed up by the Java team in the following list of buzzwords: Simple Secure Portable Object-oriented Robust 1
Simple : Java was designed to be easy for the professional programmer to learn
and use effectively. Assuming that you have some programming experience, you will not find Java hard to master. If you already understand the basic concepts of object-oriented programming, learning Java will be even easier. Best of all, if you are an experienced C++ programmer, moving to Java will require very little effort. Because Java inherits the C/C++ syntax and many of the object-oriented features of C++, most programmers have little trouble learning Java. Also, some of the more confusing concepts from C++ are either left out of Java or implemented in a cleaner, more approachable manner.
Architecture-Neutral : A central issue for the Java designers was that of code
longevity and portability. The Java Virtual Machine in an attempt to alter this situation. Their goal was write once; run anywhere, any time, forever. To a great extent, this goal was accomplished.
Interpreted and High Performance : Java enables the creation of crossplatform programs by compiling into an intermediate representation called Java bytecode. This code can be interpreted on any system that provides a Java Virtual Machine. It is true that Java was engineered for interpretation, the Java bytecode was carefully designed so that it would be easy to translate directly into native machine code for very high performance by using a just-in-time compiler.
Dynamic : Java programs carry with them substantial amounts of run-time type
information that is used to verify and resolve accesses to objects at run time. This makes it possible to dynamically link code in a safe and expedient manner.
class SUM { public static void main(String a[]) { Scanner s1=new Scanner(System.in); System.out.print("Enter the value of first number:"); int i =s1.nextInt(); System.out.print("Enter the value of second number:"); int j=s1.nextInt( ); int s=i+j; System.out.println("Sum of="+i+"and"+j+"is="+s); } }
OUTPUT :
{ public static void main(String ar[ ]) { Scanner sc=new Scanner(System.in); System.out.print("enter The limit:"); int j=sc.nextInt( ); int i,a=0,b=1,s; System.out.println("Fibonic Series is:"); System.out.println(a); System.out.println(b); for(i=0;i<j-2;i++) { s=a+b; a=b; b=s; System.out.println(s); } } } OUTPUT:
{ public static void main(String a[]) { int i,n,p=1; Scanner s=new Scanner(System.in); System.out.print("Enter the number:"); n=s.nextInt(); System.out.print("Fictorial of "+n+" is:"); for(i=1;i<=n;i++) { p=p*i; } System.out.println(p); } } Output :
PROGRAM 4- WRITE A PROGRAM TO FIND WEATHER THE GIVEN NUMBER IS PRIME OR NOT.
import java.util.Scanner; class PRIME { public static void main(String a[ ]) 7
{ int i,n; boolean flag=true; Scanner s=new Scanner(System.in); System.out.print("Enter the number:"); n=s.nextInt(); for(i=2;i<=n/2;i++) { if(n%i==0) { flag=false; break; } } if(flag) { System.out.println(""+n+" is prime number"); } else { System.out.println(""+n+" is not a prime number"); } } }
OUTPUT :
class PRIME2 { public static void main(String a[ ]) { int i,aa,b,j; boolean p=true; Scanner s=new Scanner(System.in); System.out.print("Enter the starting number:"); aa=s.nextInt(); System.out.print("\nEnter the ending number:"); b=s.nextInt( ); System.out.println("Prime numbers between" +aa+ "and" +b+ " are:"); for(i=aa;i<=b;i++) { p=true; for(j=2;j<i;j++) if(i%j == 0) { p=false; break; } if(p) System.out.print(i); System.out.print(" "); } } }
OUTPUT :
10
11
* ** *** ****
import java.util.Scanner; class Star { public static void main(String a[]) { Scanner s=new Scanner(System.in); System.out.print("enter n:"); int n=s.nextInt(); int i,j; for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { System.out.print("*"); } System.out.println(""); } } } OUTPUT :
12
13
14
PROGRAM 9- WRITE A PROGRAM TO PRINT ARRAY ELEMENTS USING ENHANCED FOR LOOP.
import java.util.Scanner; class ARRAYINIT { public static void main(String a[ ]) { Scanner s1=new Scanner(System.in); int aa[]={1,4,2,6,3}; System.out.println("Array elements are:"); for(int temp:aa) { System.out.println(temp); } } }
OUTPUT :
15
PROGRAM 10-WRITE A PROGRAM TO CREATE A CLASS AND USE A FOR STATEMENT TO ITERATE FROM 31 TO 175 IN THE MAIN METHOD WHICH PRINTS NUMBER IS A MULTIPLE OF THREE, NUMBER IS A MULTIPLE OF FIVE NUMBER IS A MULTIPLE OF SEVEN 7 RESPECTIVELY IF THE CURRENT NUMBER IS Divisible By 3, 5 Or 7 Respectively.
import java.util.Scanner; class multiple { public static void main(String ar[]) { int i; Scanner sc=new Scanner(System.in); for(i=31;i<=180;i++) { if(i%3==0) { System.out.println(i + "is multiple of 3 "); System.out.println(" "); } if(i%5==0) { System.out.println(i + "is multiple of 5 "); System.out.println(" "); } if(i%7==0) { System.out.println(i + "is multiple of 7 "); System.out.println(" "); } } } }
16
OUTPUT: 33 is multiple of 3 35 is multiple of 5 35 is multiple of 7 36 is multiple of 3 39 is multiple of 3 40 is multiple of 4 42 is multiple of 3 42 is multiple of 7 45 is multiple of 3 45 is multiple of 5 48 is multiple of 3 49 is multiple of 7 50 is multiple of 5 51 is multiple of 3 54 is multiple of 3 55 is multiple of 5 56 is multiple of 7 57 is multiple of 3 60 is multiple of 3 60 is multiple of 5 63 is multiple of 3 63 is multiple of 7 65 is multiple of 5 66 is multiple of 3 69 is multiple of 3 70 is multiple of 5 70 is multiple of 7 72 is multiple of 3 75 is multiple of 3 75 is multiple of 5 77 is multiple of 7 78 is multiple of 3 80 is multiple of 5 81 is multiple of 3 84 is multiple of 3 17
84 is multiple of 7 85 is multiple of 5 87 is multiple of 3 90 is multiple of 3 90 is multiple of 5 91 is multiple of 7 93 is multiple of 3 95 is multiple of 5 96 is multiple of 3 98 is multiple of 7 99 is multiple of 3 100 is multiple of 5 102 is multiple of 3 105 is multiple of 3 105 is multiple of 5 105 is multiple of 7 108 is multiple of 3 110 is multiple of 5 111 is multiple of 3 112 is multiple of 7 114 is multiple of 3 115 is multiple of 5 117 is multiple of 3 119 is multiple of 7 120 is multiple of 3 120 is multiple of 5 123 is multiple of 3 125 is multiple of 5 126 is multiple of 3 126 is multiple of 7 129 is multiple of 3 130 is multiple of 5 132 is multiple of 3 133 is multiple of 7 135 is multiple of 3 135 is multiple of 5 18
138 is multiple of 3 140 is multiple of 5 140 is multiple of 7 141 is multiple of 3 144 is multiple of 3 145 is multiple of 5 147 is multiple of 3 147 is multiple of 7 150 is multiple of 3 150 is multiple of 5 153 is multiple of 3 154 is multiple of 7 155 is multiple of 5 156 is multiple of 3 159 is multiple of 3 160 is multiple of 5 161 is multiple of 7 162 is multiple of 3 165 is multiple of 3 165 is multiple of 5 168 is multiple of 3 168 is multiple of 7 170 is multiple of 5 171 is multiple of 3 174 is multiple of 3 175 is multiple of 5 175 is multiple of 7
19
PROGRAM 11- WRITE A PROGRAM WHICH USES STUDENT CLASS TO READ NAMES, ROLLNO, MARKS OF A STUDENT AND DISPLAY THEIR RECORDS AND TOTAL NUMBER OF STUDENT OBJECTS CREATED IN A WELL FORMATTED FORMAT.
import java.util.Scanner; class Student { static int count=0; int rollno,marks; String name; Scanner sc=new Scanner(System.in); void getdata() { count++; System.out.println("enter the name of the student : "); name=sc.nextLine(); System.out.println(); System.out.println("enter the rollno. of the student : "); rollno=sc.nextInt(); System.out.println(); System.out.println("enter the marks of the student : "); marks=sc.nextInt(); } void display() { System.out.println(rollno + " + marks); } } class Info { public static void main(String ar[]) { Student s1,s2,s3; s1=new Student(); s2=new Student(); 20 "+ name + " "
s3=new Student(); System.out.println("ENTER INFORMATION OF 1st STUDENT"); s1.getdata(); System.out.println("ENTER INFORMATION OF 2nd STUDENT"); s2.getdata(); System.out.println("ENTER INFORMATION OF 3rd STUDENT"); s3.getdata(); System.out.println(); System.out.println("*****************************************************"); System.out.println("ROLLNO" + " " + "NAME" + "" + "MARKS"); System.out.println("*****************************************************"); s1.display(); s2.display(); s3.display(); System.out.println(Student.count + " OBJECTS CREATED"); } } OUTPUT: ENTER INFORMATION OF 1st STUDENT enter the name of the student: deepti enter the rollno. of student: 1 enter the marks of student: 200 ENTER INFORMATION OF 2nd STUDENT enter the name of the student: aman enter the rollno. of student: 2 enter the marks of student: 300 ENTER INFORMATION OF 3rd STUDENT enter the name of the student: deep 21
400
******************************************************************** ROLLNO NAME MARKS **************************************************************************************** 34 deepti 200 76 aman 300 74 deep 400 3 OBJECTS CREATED
PROGRAM 12-WRITE A PROGRAM WHICH USES OBJECTS TO READ NAMES, ROLLNO, MARKS, PERCENTAGE OF N STUDENTS AND DISPLAY THEIR RECORDS IN WELL FORMATTED FORMAT.
import java.util.Scanner; class Student1 { int rollno,marks; float per; String name; Scanner sc=new Scanner(System.in); void getdata() { System.out.println("enter the name of the student : "); name=sc.nextLine(); System.out.println(); System.out.println("enter the rollno. of the student : "); rollno=sc.nextInt(); System.out.println(); System.out.println("enter the marks of the student : "); marks=sc.nextInt(); } void display() { per=(marks/7); //System.out.println(per); System.out.println(rollno + " "+ name + " " + marks+ " "+ per); } } class Ninfo { public static void main(String ar[]) { 22
int i,n; Scanner s=new Scanner(System.in); System.out.println("Enter the number of Students="); n=s.nextInt(); Student1 s1[]=new Student1[n];//n reference variables declared for(i=0;i<n;i++) { s1[i]=new Student1(); System.out.println("enter the information of " + (i+1) + " student"); s1[i].getdata(); } System.out.println(" "); System.out.println("*********************************************************************"); System.out.println("ROLLNO" + " "+"NAME"+ "+"MARKS"+" "+"PERCENTAGE"); System.out.println("*********************************************************************"); for(i=0;i<n;i++) { s1[i].display(); } } } OUTPUT: Enter the number of students=2 enter the information of 1 student enter the name of the student: Preet enter the rollno. of the student:72 enter the marks of student: 450 enter the information of 2 student enter the name of the student:Aman enter the rollno. of the student: 73 enter the marks of student: 500 ***************************************************************************************** ROLLNO NAME MARKS PERCENTAGE ***************************************************************************************** 72 Preet 450 75.0 73 Aman 500 76.0
23
PROGRAM 13-CREATE A CLASS MATRIX TO IMPLEMENT MATRIX OPERATIONS LIKE ADDITION, SUBTRACTION, MULTIPLICATION AND TRANSPOSE.
import java.util.Scanner; class Matrix { int r,c,r1,c1,a[][],b[][],ad[][]; Scanner sc=new Scanner(System.in); Matrix() { System.out.print("enter the number of rows of first matrix: "); r=sc.nextInt(); System.out.print("enter the number of columns of first matrix: "); c=sc.nextInt(); a=new int[r][c]; System.out.print("enter the number of rows of second matrix : "); r1=sc.nextInt(); System.out.print("enter the number of columns of second matrix: "); c1=sc.nextInt(); b=new int[r1][c1]; ad=new int[r][c]; } void getdata() { System.out.println("enter the elements of first matrix : "); for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { a[i][j]=sc.nextInt(); } } System.out.println("enter the elements of second matrix : "); for(int i=0;i<r1;i++) { for(int j=0;j<c1;j++) 24
{ b[i][j]=sc.nextInt(); } } } void addition() { System.out.println(" "); System.out.println("ADDITION OF TWO MATRIX IS:"); for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { ad[i][j]=a[i][j]+b[i][j]; System.out.print(ad[i][j] +" "); } System.out.println(" "); } } void subtraction() { System.out.println(" "); System.out.println("SUBTRACTION OF TWO MATRIX IS:"); for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { ad[i][j]=a[i][j]-b[i][j]; System.out.print(ad[i][j] +" "); } System.out.println(" "); } } void transpose() { System.out.println(" "); 25
System.out.println("TRANSPOSE OF FIRST MATRIX IS:"); for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { ad[i][j]=a[j][i]; System.out.print(ad[i][j] +" "); } System.out.println(" "); } } void multiplication() { //ad[][]=0; for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { ad[i][j]=0; } } System.out.println(" "); System.out.println("MULTIPLICATION OF MATRICES IS:"); for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { for (int k=0;k<r;k++) { ad[i][j]=ad[i][j]+a[i][k]*b[k][j]; } System.out.print(ad[i][j] +" "); } System.out.println(" "); } } 26
} class Moper { public static void main(String ar[]) { Matrix obj1; obj1=new Matrix(); obj1.getdata(); obj1.addition(); obj1.subtraction(); obj1.transpose(); obj1.multiplication(); } } OUTPUT: enter the number of rows of first matrix: 2 enter the number of columns of first matrix: 2 enter the number of rows of second matrix: 2 enter the number of columns of second matrix: 2 enter the elements of first matrix : 3 5 7 9 enter the elements of second matrix: 2 4 5 6 ADDITION OF TWO MATRIX IS : 5 9 12 15 SUBTRACTION OF TWO MATRIX IS: 1 1 2 3 TRANSPOSE OF FIRST MATRIX IS: 3 7 5 9 MULTIPLICATION OF MATRICES IS: 27
31 42 59 82
PROGRAM 14-WRITE A PROGRAM TO CREATE A CLASS EMPLOYEE HAVING THE FOLLOWING MEMBERS: DATA MEMBERS: EMPLOYEENAME, SALARY DATAINPUT(), DISPLAY(), CALSALARY() THE CALSALARY() FUNCTION WILL CALCULATE THE TOTAL SALARY ACCORDING TO YEAR OF JOINING AND BASIC PAY TAKING 70% D.A., 15 % H.R.A.AND 5% MA. CREATE DIFFERENT OBJECTS OF THIS CLASS AND WORK WITH THEM.
import java.util.Scanner; class date { Scanner sc=new Scanner(System.in); int day; int month; int year; } class Emp { String ename; date doj=new date(); //doj=new date(); float basic; float total; Scanner sc=new Scanner(System.in); Emp() { basic=0; total=0; 28
YEAR-OF-JOINING,
BASIC
PAY,
TOTAL-
MEMBER FUNCTIONS:
} void inputdata() { System.out.print("enter the name of the employee : "); ename=sc.next(); System.out.print("enter the basic pay of an employee : "); basic=sc.nextInt(); System.out.print("enter the day"); doj.day=sc.nextInt(); System.out.println("enter the month : "); doj.month=sc.nextInt(); System.out.println("enter the year : "); doj.year=sc.nextInt(); } void calsalary() { float da=(basic * 70/100); float hra=(basic * 15/100); float ma=(basic * 5/100); total=basic+da+hra+ma; } void display() { +" } class Employee { public static void main(String ar[]) { Emp e1,e2,e3; e1=new Emp(); e2=new Emp(); e3=new Emp(); System.out.println("enter the information of 1st employee : "); 29 System.out.println(ename +" " +doj.day +"-"+doj.month + "-" +doj.year "+basic+" "+total); }
e1.inputdata(); e1.calsalary(); System.out.println("enter the information of 2nd employee : "); e2.inputdata(); e2.calsalary(); System.out.println("enter the information of 3rd employee : "); e3.inputdata(); e3.calsalary(); System.out.println("NAME"+" "+"DOJ"+" "+"BASIC SALARY"+" "+"TOTAL SALARY"); e1.display(); e2.display(); e3.display(); } }
OUTPUT: enter the information of 1st employee : enter the name of the employee:Aman enter the basic pay of an employeE : 1000 enter the day 8 enter the month :5 enter the year : 2005 enter the information of 2nd employee : enter the name of the employee:Deepti enter the basic pay of an employee : 1100 enter the day 20 enter the month :5 enter the year : 2006 enter the information of 3rd employee : enter the name of the employee:Deepak enter the basic pay of an employee : 1200 enter the day 22 enter the month :6 enter the year : 2007 30
PROGRAM 15-WRITE A PROGRAM TO CREATE CLASS EMPLOYEE AS DEFINED IN THE ABOVE PROGRAM AND ADD A STATIC DATA MEMBER TOTALPAYMENTS. USE A CONSTRUCTOR TO INITIALIZE BASIC PAY AND A STATIC FUNCTION TO ADD SALARY OF EACH EMPLOYEE TO THE TOTAL PAYMENTS AND DISPLAY IT.
import java.util.Scanner; class date1 { Scanner sc=new Scanner(System.in); int day; int month; int year; } class Emp1 { String ename; date1 doj=new date1(); float basic; static float total=0; Scanner sc=new Scanner(System.in); Emp1() { basic=0; } void inputdata() { System.out.print("enter the name of the employee : "); ename=sc.next(); System.out.print("enter the basic pay of an employee : "); basic=sc.nextInt(); 31
System.out.print("enter the day"); doj.day=sc.nextInt(); System.out.println("enter the month : "); doj.month=sc.nextInt(); System.out.println("enter the year : "); doj.year=sc.nextInt(); total=total+basic; } static void totalpay() { System.out.println("TOTAL PAYMENT ="+total); } void display() { System.out.println(ename +" +doj.year +" "+basic); } } class Employee1 { public static void main(String ar[]) { Emp1 e1,e2; e1=new Emp1(); e2=new Emp1(); System.out.println("enter the information of 1st employee : "); e1.inputdata(); //Emp1.calsalary(); System.out.println("************************************************"); System.out.println("enter the information of 2nd employee : "); e2.inputdata(); //Emp1.calsalary(); System.out.println(" "); System.out.println("************************************************"); System.out.println("NAME"+" "+"BASIC"); "+"DOJ"+" " +doj.day +"-"+doj.month + "-"
System.out.println("************************************************"); 32
e1.display(); e2.display(); System.out.println(" "); Emp1.totalpay();//call static function } } OUTPUT: enter the information of 1st employee : enter the name of the employee: deepti enter the basic pay of an employee: 10000 enter the day 11 enter the month : 2 enter the year:2001 enter the information of 2nd employee : enter the name of the employee: aman enter the basic pay an employee : 20000 enter the day :12 enter the month :3 enter the year:2002 ************************************************************************NAME DOJ BASIC ************************************************************************deepti 11-2-2001 10000.0 aman 12-3-2002 20000.0 TOTAL PAYMENTS =30000.0
33
PROGRAM 16-WRITE A PROGRAM TO INITIALIZE EMPLOYEE DATA IN THE EMPLOYEE CLASS THROUGH PARAMETRIZED CONSTRUCTOR.
class Emp2 { int eid; String ename,dept; float basic; Emp2() { eid=1; basic=0; } Emp2(int id,String name,String dep,float bas) { eid=id; ename=name; dept=dep; basic=bas; } void display() { System.out.println(eid +" "+basic); } } class employee2 { public static void main(String ar[]) { Emp2 e1,e2; e1=new Emp2(); e2=new Emp2(2272,deepti,production,25000); e2.display(); } } 34 " +ename+" "+ dept + "
PROGRAM 17-WRITE A PROGRAM TO CREATE AN ARRAY OF OBJECTS OF THE EMPLOYEE CLASS. INPUT DATA ABOUT EACH EMPLOYEE IN A WELL FORMATTED WAY AND CALL CALSALARY FUNCTION THROUGH DISPLAY FUNCTION. CALSALARY MUST NOT BE VISIBLE OUTSIDE THE CLASS.
import java.util.scanner; class Emp3 { int eid; String ename,dept; float basic,total=0; Scanner sc=new Scanner(System.in); void inputdata() { System.out.print("enter the name of an employee : "); ename=sc.next(); System.out.print("enter the department of an employee : "); dept=sc.next(); System.out.print("enter the ID of the employee : "); eid=sc.nextInt(); System.out.print("enter the basic pay of an employee : "); basic=sc.nextInt(); } void calsalary() { float da=(basic * 70/100); float hra=(basic * 15/100); float ma=(basic * 5/100); total=basic+da+hra+ma; } void display() { 35
calsalary(); System.out.println(eid +" "+ basic +" "+ total); } } class employee3 { public static void main(String ar[]) { int i,n; Scanner s=new Scanner(System.in); System.out.print("Enter the number of Employees="); n=s.nextInt(); Emp3 e1[]=new Emp3[n];//n reference variables declared for(i=0;i<n;i++) { e1[i]=new Emp3(); System.out.println("enter the information of " + (i+1) + " employee"); e1[i].inputdata(); } System.out.println(" "); System.out.println("*********************************************************************** ***************"); System.out.println("EID" + " "+"DEPARTMENT"+" "+"ENAME"+" "+"BASIC"+" "+"TOTAL"); " + ename +" "+ dept +"
System.out.println("************************************************************************ **************"); for(i=0;i<n;i++) { e1[i].display(); } } } OUTPUT: Enter the number of Employees =2 enter the information of 1st employee
36
enter the name of the employee : deepti enter the department of an employee : accounts enter the ID of an employee : 101 enter the basic pay of an employee : 20000 enter the information of 2nd employee enter the name of the employee : aman enter the department of an employee : production enter the ID of an employee : 104 enter the basic pay of an employee : 30000 ********************************************************************************* EID 101 104 ENAME deepti aman DEPARTMENT accounts production BASIC 20000 30000 38000 57000 TOTAL *********************************************************************************
37