Sie sind auf Seite 1von 13

Core JavaProgram

KUVEMPU UNIVERSITY JAVA Programming 4th Semister


(Solved paper)
Q1) Write a program to printWelcome to java Programming in the screen. Ans: //Sample.java import java.io.*; class sample { public static void main(String args[])throws IOException { String str; BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Text"); str=b.readLine(); System.out.println("The text is=="+str); } } //WAP to print the text Welcome to Java Programming class samplejava { public static void main(String args[]) { System.out.println("Welcome to Java Programming"); } } Q2) Write a program to find the greatest of the given three numbers. Ans: // greatest.java import java.io.*; class greatest { public static void main(String args[])throws IOException { int i,j,k; BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
VGSL JavaProgramming(Practical Solved)

System.out.println("Enter First Number="); i=Integer.parseInt(br.readLine()); System.out.println("Enter The second Number="); j=Integer.parseInt(br.readLine()); System.out.println("Enter the third number="); k=Integer.parseInt(br.readLine()); if((i>j)&&(i>k)) System.out.println("The greater number is:"+i); else if ((j>i)&&(j>k)) System.out.println("The greater number is:"+j); else System.out.println("The greater number is:"+k); } } Q3)Write a program by using the switch case add,substract,multiply and division of two numbers. Ans: //switchDemo.java import java.io.*; import java.lang.*; class switchDemo { public static void main(String[] args)throws IOException { int i, j,op; BufferedReader b= new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter first number"); i=Integer.parseInt(b.readLine()); System.out.println("enter second number"); j=Integer.parseInt(b.readLine()); System.out.println("enter operator 1 for +,2 for -,3 for * and 4 for /"); op=Integer.parseInt(b.readLine()); switch(op) { case 1: System.out.println("Addition of two number is:" +(i+j)); break; case 2: System.out.println("substraction of two number is:"+(i-j)); break;

Core JavaProgram

case 3: System.out.println("multiplication of two number is:"+(i*j)); break; case 4: System.out.println("Division of two number is:" +(i/j)); break; default: System.out.println("Invalid Number"); break; } } } Q4)Write a Program by using for loop for adding numbers up to 50 Ans: //forDemo.java import java.io.*; class forDemo { public static void main(String args[])throws IOException { int i,n; BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); n=Integer.parseInt(b.readLine()); Ssystem.out.println("Enter the number:"); for(i=0;i<=n;i++) { System.out.println("Result is:"+i); } } } Q4)Write a program by using for loop to add the integer up to 50 and print the result on the screen. //loopsample.java class loopsample { public static void main(String args[]) { int i; for(i=0;i<=50;i++) { System.out.println(+i);
VGSL Java programming(Practical Solved)

} } Q5)Write a program to find the largest element in an array. Ans: //arrayDemo.java import java.lang.*; public class arrayDemo { public static void main(String args[]) { int arr[]; arr=new int[]{55,88,99}; int large,i; large=arr[0]; for(i=1;i<3;i++) { if(arr[i]>large) large=arr[i]; } System.out.print(The Largest element the array is:+large); } } Q6)Write a program for find out the even sum and odd sum in an given array. Ans: //WAP to find out the even and odd sum of from given numbers //loopsum.java import java.io.*; class loopsum { public static void main(String args[])throws IOException { int i,n,s=0,t=0; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the number u want"); n=Integer.parseInt(br.readLine()); for(i=1;i<=n;++i) { if((i%2)==0) {

Core JavaProgram

System.out.println("Even no"+i); s=s+i; } if((i%2)==1) { System.out.println("odd no"+i); t=t+i; } } System.out.println("Sum of even no is="+s); System.out.println("Sum of odd no is="+t); } } Q7) Write a program to add two matrices.
Ans://addmatrix.java import java.io.*;

VGSL

Java programming(Practical Solved)

class addmatrix { public static void main(String args[])throws IOException { int i,j,m,n,o,p; int a[][]=new int[10][10]; int b[][]=new int[10][10]; int c[][]=new int[10][10]; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the first order of matrix:"); n=Integer.parseInt(br.readLine()); m=Integer.parseInt(br.readLine()); System.out.println("Enter the Elements of matrix:"); for(i=1;i<=n;++i) { for(j=1;j<=m;++j) { a[i][j]=Integer.parseInt(br.readLine()); } } System.out.println("Enter the second order of matrix:"); o=Integer.parseInt(br.readLine()); p=Integer.parseInt(br.readLine()); System.out.println("Enter the Elements of second matrix:"); for(i=1;i<=o;++i) { for(j=1;j<=p;++j) { b[i][j]=Integer.parseInt(br.readLine()); } }

Core JavaProgram

System.out.println("Display the first matrix:"); for(i=1;i<=n;++i) { for(j=1;j<=m;++j) { System.out.print(+a[i][j]+" "); } System.out.println(); } System.out.println("Display the second matrix"); for(i=1;i<=o;++i) { for(j=1;j<=p;++j) { System.out.print(+b[i][j]+" "); } System.out.println(); } if((n==o)&&(m==p)) System.out.println("The addition result of two matrix is :"); for(i=1;i<=n;++i) { for(j=1;j<=m;++j) { c[i][j]=a[i][j]+b[i][j]; } } for(i=1;i<=n;++i) { for(j=1;j<=m;++j) { System.out.print(+c[i][j]+" "); } System.out.println(); } } } Q8)Write a class Rectangle and which consists of function getdata(),and area() and also write a main function to create a rectangle object and find the area by calling function area() in reactangle class and display the result in the screen. Ans: //Rectangle.java
VGSL Java programming(Practical Solved)

import java.io.*; class Rectangle { int height; int width; int area; void get_data()throws IOException { BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the width:"); width=Integer.parseInt(b.readLine()); System.out.println("Enter the width:"); height=Integer.parseInt(b.readLine()); } void area() { area=width*height; System.out.println("The Volume of the Rectangle is:"+area); } } class rectangledemo { public static void main(String args[])throws IOException { Rectangle rct=new Rectangle(); rct.get_data(); rct.area(); } } Q9)Write a class called box with the variable width,depth and height and constructor to assigning the values for these variables and a methods to find a volume of the box and also write the main program,Which create the box object and find the volume of the box. Print the result on the screen. //Box.java class Box { int height;

Core JavaProgram

int width; int depth; int vol; Box(int h,int w,int d) { height=h; width=w; depth=d; } void volume() { vol=width*height*depth; System.out.println("The Volume of the Box is:"+vol); } } class Boxdemo1 { public static void main(String args[]) { Box b=new Box(15,67,98); Box b1=new Box(5,2,6); b.volume(); b1.volume(); } } Q10)Write a program to demonstrate the method overloading for add() functions. Ans: //overloaddemo.java import java.io.*; class overloaddemo { static float add(int i,int j) { return i+j; } static float add(float t1,float t2) { return t1+t2;
VGSL Java programming(Practical Solved)

} static float add(int i,int j,float x) { return i+j+x; } public static void main(String args[])throws IOException { int i,j; float x,y; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter first integer number ="); i=Integer.parseInt(br.readLine()); System.out.println("Enter second integer number ="); j=Integer.parseInt(br.readLine()); System.out.println("Enter first float number ="); x=Float.parseFloat(br.readLine()); System.out.println("Enter second float number ="); y=Float.parseFloat(br.readLine()); System.out.println("The addition of two integer numbers is ="+add(i,j)); System.out.println("The addition of two float numbers is ="+add(x,y)); System.out.println("The addition of two int and one float no is ="+add(i,j,x)); } } Q11) Write a program to Demonstrate the method Overridding. Ans: a) //Superclass.java class Superclass { void addnum(int x,int y) { int sum; sum=x+y; System.out.println("Sum of numbers ="+sum); } void display() { System.out.println("This is superclass"); } } b) //Subclass.java

Core JavaProgram

11

class Subclass extends Superclass { void display() { System.out.println("This is subclass"); } }

c) override.java class override { public static void main(String args[]) { Subclass s1=new Subclass(); s1.addnum(10,40); s1.display(); } } Q12) Write a program to concatnate two strings. Ans: //strconcate.java import java.io.*; import java.lang.String; class strconcate { public static void main(String args[])throws IOException { String s1,s2,s3; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter First String"); s1=br.readLine(); System.out.println("Enter second String"); s2=br.readLine(); s3=s1+s2; System.out.println("The concatenate of two string is:"+s3); } }
VGSL Java programming(Practical Solved)

Q13) Write a program to find whether the two given strings are equal or not. Ans: //strdemo.java import java.io.*; import java.lang.String; class strdemo { public static void main(String args[])throws IOException { String s1,s2; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter First String"); s1=br.readLine(); System.out.println("Enter second String"); s2=br.readLine(); if(s1.equals(s2)) { System.out.println("The Two strings are equal"); } else { System.out.println("The two strings are not equal"); } } } Q14) Write a program to change the case of the given string. Ans: //strchangecase.java import java.io.*; class strchangecase { public static void main(String args[])throws IOException { String s1,upper,lower;char ch=0; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter First String"); s1=br.readLine(); upper=s1.toUpperCase();

Core JavaProgram

13

System.out.println("Srting in Uppercase is :"+s1.toUpperCase()); lower=s1.toLowerCase(); System.out.println("Srting in Uppercase is :"+s1.toLowerCase()); } }

Q15)Create a class by extending thread class. Ans: //myThread.java public class myThread extends Thread { public void run() { System.out.println("myThread Name is:"+getName()); } public static void main(String args[]) { myThread t1=new myThread(); myThread t2=new myThread(); myThread t3=new myThread(); t1.start(); t2.start(); t3.start(); } }

VGSL

Java programming(Practical Solved)

Das könnte Ihnen auch gefallen