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 10-digit string as a typical U.S
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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 10-digit 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: 1234567890You entered: 1234567890The area code is: 123The exchange code is: 456The house number is: 7890The complete telephone number is: (123)456-7890
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 ClintonWhich produces the outputClinton, 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
TanmayTanmayTanmay
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 string is: Call me ISHMAELThe length of string is: 15The character at first poition is: CThe character at last poition is: LThe 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("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: 1234567890You entered: 1234567890The area code is: 123The exchange code is: 456The number is: 7890The complete telephone number is: (123)456-789
Enter telephone number: 264859Wrong Number
Program 5
import java.io.*;class Name_form{
public static void main(String[] args) throws IOException{
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 random number generated is: 462The number is divisible by 2The number is divisible by 3The number is divisible by 6
The random number generated is 390The number is divisible by 2The number is divisible by 3The number is divisible by 5The number is divisible by 6The number is divisible by 10The number is divisible by 15The number 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 length in Meters is: "+m);system.out.println("The length in Centimeters is: "+cm);system.out.println("The length in Millimeters is: "+mm);
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: 100Enter the upper limit: 400Enter a number between 100 and 400: 380The random value is: 246You 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);