Java Final Submission (java Programs)

  • Uploaded by: TB
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Java Final Submission (java Programs) as PDF for free.

More details

  • Words: 2,346
  • Pages: 51
Java Programs List 1.

Write and run a Java program that initializes a string object with your first name and then prints it on three separate lines.

2.

Write and run a Java program that initializes a string object with your first name and then prints it three times on the same line, separated by spaces... Ram Ram Ram

3.

Write and run a Java program that does the following: Declare a string object named as s containing the string "CALL ME ISHMAEL". Print the entire string. Use the length() method to print the length of the string. Use the charAt() method to print the first character of the string. Use the charAt() and the length() methods to print the last character of the string. Use the indexOf() and the substring() methods to print the first word in the string.

4.

Write and run a Java program that enters a 10digit string as a typical U.S telephone number, extract the 3-digit area code, the 3-digit exchange code and the remaining 4-digits as the house number as a separate strings, print them, and then prints the complete telephone number in the usual formatting. A sample run might look like this:

Enter a 10-digit telephone number: 1234567890 You entered: 1234567890 The area code is: 123 The exchange code is: 456 The house number is: 7890 The complete telephone number is: (123)4567890 5.

Write and run a Java program that inputs a persons name in the form First Middle Last and then prints it in the form Last, First M. where M is the person's middle name initial. For example: Input: William Jefferson Clinton Which produces the output Clinton, William J.

6.

Write a program that reads three strings interactively and then prints them in the order of their lengths.

7.

Write and run a Java program that reads three strings interactively and then prints them in lexicographic (i.e. dictionary) order.

8.

Write a Java program that capitalizes a two word name. For example, the string literal noRtH CARolina would produce the output North Carolina.

9.

Write and run a Java program that inputs an integer that represents a temperature on the Fahrenheit scale and then computes and prints its equivalent Celsius values. Use the conversion formula C=5(F-32)/9.

10. Write and run a Java program that inputs an integer that represents a temperature on the Celsius scale and then computes and prints its equivalent Fahrenheit value. Use the conversion formula F=1.8C+32. 11. Write and run a Java program that generates a random integer, test whether it is positive, and report that it is if it is. 12. Write and run a Java program that generates a random number between 2 and 600 inclusive, and then uses nested if...else statements to determine whether it is divisible by 2, 3, 5, 6, 10, 15, and/or 30. 13. Write and run a Java program to sort the array first, then copy elements of array into another using the System.arraycopy() API. 14. Write and run a Java program that initializes an integer variable n with the value 5841 and then uses the quotient and remainder operators to extract and print each digit of n. The digits of n are 5, 8, 1, 4. 15. Define a class, Mcmlength, to represent a length measured in meters, centimeters, and millimeters, each stored as integers. Include constructors that accepts three arguments in millimeters, one double arguments in centimeters, and no arguments, which create

an object with the length set to zero. Check the class operation. 16. Write an application which does the following: Reads a character and display its ASCII code. Reads a sentence and print the sentence with all uppercase letters change to lowercase letters and all lowercase letters into uppercase letters. Reads a sentence and prints out the sentence in reverse order. 17. Write a Java program to define a class Box with its variables as the box's dimension and functions to set the dimension, to calculate the volume and to display all the information about the box. 18. Write a Java program to create a game in which ask user to enter a number and match it with the number generated randomly by the system and if number matches the then the user wins else lose. 19. Write a GUI application in Java to simply take user's name as input and display it with a message. 20. Write a GUI application Java code which adds two numbers using dialog boxes which have some text message. 21. Write a GUI application java code which converts Fahrenheit temperature into Celsius.

Program 1 class Name_prnt1 { public static void main(String[] args) { int i; String s1=new String("Tanmay"); for(i=0;i<3;i++); { System.out.print(s1); System.out.print("\n");

} } }

Output Tanmay Tanmay Tanmay

Program 2 class Name_prnt2 { public static void main(String[] args) { int i; String s1=new String("Tanmay"); for(i=0;i<3;i++); { System.out.print(s1); System.out.print("\t"); }

} }

Output Tanmay

Tanmay

Tanmay

Program 3 class Ring_fun { public static void main(String[] args) { int len,ind; String str=new String("Call me ISHMAEL"); System.out.print("The string is: "+str); len=str.length(); System.out.println("The length of the string is: "+len); System.out.println("The character at first position is: "+str.char At(0); System.out.println("The character at last position is: "+str.char At(len-1);

ind=str.indexOf(" "); System.out.println("The first word of string is: "+str.substring(0,ind)); } }

Output The The The The The

string is: Call me ISHMAEL length of string is: 15 character at first poition is: C character at last poition is: L first word of string is: Call

Program 4 import java.io.*; class Tel_no { public static void main(String[] args)throws IOException { int len; BufferedReader tel=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter telephone number: "); String t=tel.readLine(); System.out.println("You entered: "+t); len=t.length();

if(len==10) { String t1=t.substring(0,3); String t2=t.substring(3,6); String t3=t.substring(6,len); System.out.println("The area code is: "+t1); System.out.println("The exchange code is: "+t2); System.out.println("The number is: "+t3); System.out.println("The complete telephone number is: "("+t1")"+t2+"-"+t3); } else { System.out.println("Wrong Number"); } } }

Output Your telephone number: 1234567890 You entered: 1234567890 The area code is: 123 The exchange code is: 456 The number is: 7890 The complete telephone number is: (123)456-789 Enter telephone number: 264859 Wrong Number

Program 5 import java.io.*; class Name_form { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter your name: "); String name=br.readLine(); String[] n=new String[3]; n=new.split(" "); System.out.println("The name is: "+n[2]+","+n[0]+" "+n[1].charAt(0)+"."); } }

Output Enter your name: William Jefferson Clinton The name is: Clinton, William J.

Program 6 import java.io.*; class Bigger { public static void main(String[] args) throws IOException { BufferedReader name=new BufferedReader(new InputStreamReadere(System.in)); System.out.println("Enter first string: "); String n1=name.readLine(); System.out.println("Enter second string: "); String n2=name.readLine(); System.out.println("Enter third string: "); String n3=name.readLine();

int len1=n1.length(); int len2=n2.length(); int len3=n3.length(); if((len1>len2)&&(len1>len3)) { System.out.println("Biggest string: "+n1); if(len2>len3) { System.out.println("Next string: "+n2); System.out.println("last string: "+n3); } else { System.out.println("Next string: "+n3); System.out.println("Next string: "+n2); } } else if(len2>len3)&&(len2>len1) { System.out.println("Biggest String: "+n2); if(len1>len3) { System.out.println("Next string: "+n1); System.out.println("Next string: "+n3); } else { System.out.println("Next string: "+n3); System.out.println("Next string: "+n1); } } else if(len3>len2)&&(len3>len1) { System.out.println("Biggest String: "+n3); if(len2>len1) { System.out.println("Next string: "+n2); System.out.println("Next string: "+n1); } else { System.out.println("Next string: "+n1);

System.out.println("Next string: "+n2); } } } }

Output Enter first string: Hello! Enter second string: everybody Enter third string is: welcome The biggest string is: everybody The next string is: welcome The last string is: Hello!

Program 7 class Str_order { static String name[]={"Tanmay","Chinmay","Ravi","Daksh"}; public static void main(String[] args) { int size=name.length; String temp=null; for(int i=0;i<size;i++) { for(int j=i+1;j<size;j++) { if(name[j].compareTo(name[i])<0) { temp=name[i]; name[i]=name[j]; name[j]=temp; }

} } for(int i=0;i<size;i++) { String.out.println()name[i]; } } }

Output Chinmay Daksh Ravi Tanmay

Program 8 import java.util.Random; import java.io.*; class Case { public static void main(String[] args)throws IOException { BufferedReader lit=new BufferedReader(new InputStreamReader(System.in)); System.out.print("\nEnter a two word string literal: "); String L=lit.readLine(); int len=Length(); String s=L.substring(0,1); String s1=s.toUpperCase(); int a=L.intexof(''); String s2=L.substring(1,a); String s3=s2.toLowerCase(); String s4=L.substring(a+1,a+2);

String s5=s4.toUpperCase(); String s6=L.substring(a+2,len); String s7=s6.toLowerCase(); System.out.println("The string in the format is: "+s1+s2+" " +s5+s7); } }

Output Enter a two word string literal: noRtH CARolina The string in the format is: North Carolina

Program 9 import java.util.Random; import java.io.*; class Temp_cel { public static void main(String[] args)throws IOException { BufferedReader temp=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter temperature in Fahrenheit: "); int f=Integer.parseInt(temp.readLine()); double c=5*(f-32)/9; System.out.print("The temperature in Celsius is: "+c); } }

Output Enter the temperature in Fahrenheit: 100 The temperature in Celsius is: 37.0

Program 10 import java.util.Random; import java.io.*; class Temp_far { public static void main(String[] args)throws IOException { BufferedReader temp=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter temperature in Celsius: "); int c=Integer.parseInt(temp.readLine()); double f=(1.8*c)+32; System.out.print("The temperature in Fahrenheit is: "+f); } }

Output Enter temperature in Celsius: 37 The temperature in Fahrenheit is: 98.6

Program 11 import java.util.Random; class Rndm_test { public static void main(String[] args) { Random r=new Random(); int n=r.nextInt(); System.out.println(n); if(n>0) { System.out.println("The random number generated is positive!"); } else { System.out.println("The random number generated is negative!"); } } }

Output 1864883468 The random number generated is positive! -16754937516 The random number generated is negative!

Program 12 import java.util.Random; class Rndm_div { public static void main(String []args) { Random r=new Random(); float j=r.nextFloat(); int g=Math.round(j*(600-2)+2); System.out.print("/nThe random number generated is:"); System.out.print(g); if(g%2==0) { System.out.print("/nThe number is divisible by 2"); } if(g%3==0) { System.out.print("/nThe number is divisible by 3"); } if(g%5==0) { System.out.print("/nThe number is divisible by 5");

} if(g%6==0) { System.out.print("/nThe number is divisible by 6"); } if(g%10==0) { System.out.print("/nThe number is divisible by 10"); } if(g%15==0) { System.out.print("/nThe number is divisible by 15"); } if(g%30==0) { System.out.print("/nThe number is divisible by 30"); } } }

Output The The The The

random number number number

number generated is: 462 is divisible by 2 is divisible by 3 is divisible by 6

The The The The The The The The

random number number number number number number number

number generated is 390 is divisible by 2 is divisible by 3 is divisible by 5 is divisible by 6 is divisible by 10 is divisible by 15 is divisible by 30

Program 13 class Arr_sort { public static void main(string[] args) { int i,j,temp=0; int arr[]={55,40,80,65,71}; int n=arr.length; int brr[]=new int[n]; System.out.println("The given array is: "); for(i=0;i=0&&arr[j]>temp;j--) { arr[j+1]=arr[j]; arr[j]=temp; } } System.arraycopy(arr,0,brr,0,n); System.out.println("The sorted array is: "); for(i=0;i
System.out.println("\n"); } }

Output The list of array is: 34 85 49 67 25 The list of sorted array is: 25 34 49 67 85

Program 14 class Extract { public static void main(String[] args) { int n=5841; int a[]=new int[5]; int r,f=0; for(n=n;n!=0;n=n/10) { r=n%10; a[f]=r; f++; } f=f-1; for(int i=f;i>=0;i--) { System.out.print(" \t"+a[i]); } } }

Output The digits of n are:

5

8

4

1

Program 15 class Mcmlength { private double m; private double cm,c1; private double mm,m1; private int i; Mcmlength((int a, int b, intx= c) { if (a>=1000||b>=1000||c>=1000) { m=(a/1000)+(b/1000)+(c/1000); } m1=(a%10)+(b%10)+(c%10); mm=m1%10; if (a>=10||b>=10||c>=10) { c1=((a%1000)/10)+(b/10)+(c/10)+(m1/10); cm=(int)c1; } } Mcmlength(double length) ( i=(int)length; m=(length-(length%100))/100; cm=(length-(m*10))-(length-i); mm=length-i; ) Mcmlength() { m=0; cm=0;

mm=0; } void printlength() { system.out.println("The length in Meters is: "+m); system.out.println("The length in Centimeters is: "+cm); system.out.println("The length in Millimeters is: "+mm); } } class Length { public static void main(String[] args) { Mcmlength 11=mew Mcmlength(); Mcmlength 12=mew Mcmlength(2587.63); Mcmlength 13=mew Mcmlength(3462,595,85); 11.printlength(); 12.printlength(); 13.printlength(); } }

Output The length in Meters is: 0.0 The length in Centimeters is: 0.0 The length in Millimeters is: 0.0 The length in Meters is: 25.0 The length in Centimeters is: 87.0 The length in Millimeters is: .6299999999998909 The length in Meters is: 4.0 The length in Centimeters is: 14.0 The length in Millimeters is: 2.0

Program 16 import java.lang.Character.*; import java.lang.String; import java.io.*; class Aschar { public static void main(String[] args)throws IOException { int i; BufferedReader sen=new BufferedReader(new InputStreamReader(System.in)); System.out.print("\nEnter a character:"); String a=sen.readLine(); int v1=(int)a.charAt(0); System.out.print("\nThe ASCII value of "+a+ " is:"+v1); System.out.print("\n\nEnter a sentence:"); String s1=sen.readLine(); int l=s1.length(); int b; char ch[]=new char[l]; for(i=0;i=65)&&(b<=90)) { b=b+32; ch[i]=(char)b; } else if((b>=97)&&(b<=122)) { b=b-32; ch[i]=(char)b;

} } System.out.print("\nThe modified sentence is: "); for(i=0;i=0;i--) { ch1[p]=s2.charAt(i); p++; } for(i=0;i
Output Enter a character: r The ASCII value of r is: 114 Enter a sentence: mY FrIEnds cAll mE TaNy The modified sentence is: My fRieNDS CaLL Me tAnY Enter a sentence: Daksh is my Nephew The sentence in reverse order is: wehpeN ym si hskaD

Program 17 import java.util.Random; import java.io.*; class Que_17 { public static void main(String[] arg) throws IOException { BufferedReader val=new BufferedReader(new InputStreamReader(System.in)); System.out.print("\nEnter the limit of the value to be generated. "); System.out.print("\nEnter the lower limit: "); int l1=Integer.parseInt(val.readLine()); System.out.print("\nEnter the upper limit: "); int u1=Integer.parseInt(val.readLine()); System.out.print("\nEnter a number between "+l1+" and "+u1+": "); int v1=Integer.parseInt(val.readLine()); Random r=new Random(); float j=r.nextFloat(); int g=Math.round(j*(u1-l1)+l1); System.out.print("\nThe random value is: "+g); if(v1==g) { System.out.print("\nYou Won!!"); } else { System.out.print("\nYou Lost!!"); } } }

Output Enter the limit of the value to be generated. Enter the lower limit: 100 Enter the upper limit: 400 Enter a number between 100 and 400: 380 The random value is: 246 You Lost!!

Program 18 import javax.swing.*; class Que_18 { public static void main(String[] args) { JOptionPane.showMessageDialog(null, “This is a GUI application!"); String n=JOptionPane.showInputDialog(null, “What is your name: "); JOptionPane.showMessageDialog(null, “Your name is: "+n); } }

Output

Program 19 import javax.swing.*; class Que_19 { public static void main(String[] args) { JOptionPane.showMessageDialog(null,"Addition"); JOptionPane.showMessageDialog(null,"Enter two numbers!"); String n1=JOptionPane.showInputDialog(null,"Enter first number :"); int a=Integer.parseInt(n1); String n2=JOptionPane.showInputDialog(null,"Enter second number:"); int b=Integer.parseInt(n2); int n=a+b; JOptionPane.showMessageDialog(null,"The sum of two numbers is:"+n); } }

Output

Program 20 import javax.swing.*; class Que_20 { public static void main(String[] args)

{ String n1= JOptionPane.showInputDialog (null,"ENTER THE FIRST NUMBER:"); String n2 =JOptionPane.showInputDialog (null,"ENTER THE SECOND NUMBER:"); int num1=Integer.parseInt(n1); int num2=Integer.parseInt(n2); String msg; if(num1>num2) { msg=String.format("THE FIRST NUMBER IS GREATER ie: %d",num1); } else { msg=String.format("THE SECOND NUMBER IS GREATER ie: %d",num2); JOptionPane.showMessageDialog(null,msg); } } }

Output

Program 21 import javax.swing.*; class Que_21

{ public static void main(String[] args) { String F=JOptionPane.showInputDialog (null,"ENTER THE TEMPERATURE IN FAHRENHEIT:"); int F1=Integer.parseInt(F); int C=5*(F1-32)/9; String msg=String.format("THE TEMPERATURE IN CELSIUS IS: %d",C); JOptionPane.showMessageDialog(null,msg); } }

Output

Related Documents

Final Java
November 2019 11
Java Java
June 2020 44
Java
November 2019 24
Java
November 2019 26
Java
December 2019 27

More Documents from "Suyanto"