SlideShare a Scribd company logo
JAVA Question : Programming Assignment
1) Write a Java Program to reverse a string without
using String inbuilt function.
Following program code explains this:
public class FinalReverseWithoutUsingStringMethods {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "Automation";
StringBuilder str2 = new StringBuilder();
str2.append(str);
str2 = str2.reverse(); // used string builder to reverse
System.out.println(str2);
}
}
2) Write a Java Program to reverse a string without using
String inbuilt function reverse()
Answer: There are several ways with which you can reverse your string if you are allowed to use the
other string inbuilt functions.
In this method, we are initializing a string variable called str with the value of your given string. Then,
we are converting that string into a character array with the toCharArray() function. Thereafter, we are
using for loop to iterate between each character in reverse order and printing each character.
public class FinalReverseWithoutUsingInbuiltFunction {
public static void main(String[] args) {
String str = "Saket Saurav";
char chars[] = str.toCharArray(); // converted to character array and printed in reverse order
for(int i= chars.length-1; i>=0; i--) {
System.out.print(chars[i]);
}
}
}
3) Write a Java Program to swap two numbers using the
third variable.
import java.util.Scanner;
public class SwapTwoNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x, y, temp;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println("Before Swapping" + x + y);
temp = x;
x = y;
y = temp;
System.out.println("After Swapping" + x + y);
}
}
4) Write a Java Program to swap two numbers without using
the third variable.
import java.util.Scanner;
class SwapTwoNumberWithoutThirdVariable
{
public static void main(String args[])
{
int x, y;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println("Before Swappingnx = "+x+"ny = "+y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After Swapping without third variablenx = "+x+"ny = "+y);
}
}
5) Write a Java Program to count the number of words in a
string using HashMap.
import java.util.HashMap;
public class FinalCountWords {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "This this is is done by Saket Saket";
String[] split = str.split(" ");
HashMap<String,Integer> map = new HashMap<String,Integer>();
for (int i=0; i<split.length; i++) {
if (map.containsKey(split[i])) {
int count = map.get(split[i]);
map.put(split[i], count+1);
}
else {
map.put(split[i], 1);
}
}
System.out.println(map);
}
}
JAVA Question : Programming Assignment

More Related Content

PDF
Prompt a user to enter a series of integers separated by spaces and .pdf
PPTX
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
PDF
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
PPTX
Lab01.pptx
PPTX
Lab101.pptx
DOCX
PPTX
Computer programming 2 chapter 1-lesson 2
PDF
Simple 27 Java Program on basic java syntax
Prompt a user to enter a series of integers separated by spaces and .pdf
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
Lab01.pptx
Lab101.pptx
Computer programming 2 chapter 1-lesson 2
Simple 27 Java Program on basic java syntax

Similar to JAVA Question : Programming Assignment (18)

PDF
Simple Java Program for beginner with easy method.pdf
PDF
Object Oriented Solved Practice Programs C++ Exams
DOC
Java final lab
ODT
Java practical
PDF
Java programming lab manual
PDF
djkkfhulkgyftfdtrdrsdsjjjjjjjjjjjjjjjjjjj
PDF
3.Lesson Plan - Input.pdf.pdf
PDF
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
PDF
java programming cheatsheet
PDF
The java program that prompts user to enter a string and .pdf
PDF
Oot practical
DOCX
Java file
DOCX
Java file
DOCX
Creating Interface- Practice Program 6.docx
PDF
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
PPTX
Lecture-5Programming WorldJava Lecture.pptx
PDF
import java.util.Scanner;public class ArrayOperation {    inp.pdf
PDF
import java.util.;public class Program{public static void.pdf
Simple Java Program for beginner with easy method.pdf
Object Oriented Solved Practice Programs C++ Exams
Java final lab
Java practical
Java programming lab manual
djkkfhulkgyftfdtrdrsdsjjjjjjjjjjjjjjjjjjj
3.Lesson Plan - Input.pdf.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
java programming cheatsheet
The java program that prompts user to enter a string and .pdf
Oot practical
Java file
Java file
Creating Interface- Practice Program 6.docx
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Lecture-5Programming WorldJava Lecture.pptx
import java.util.Scanner;public class ArrayOperation {    inp.pdf
import java.util.;public class Program{public static void.pdf
Ad

Recently uploaded (20)

PPTX
ACUTE NASOPHARYNGITIS. pptx
PPTX
How to Manage Loyalty Points in Odoo 18 Sales
PPTX
How to Manage Bill Control Policy in Odoo 18
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PPTX
IMMUNIZATION PROGRAMME pptx
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
PPTX
Congenital Hypothyroidism pptx
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
High Ground Student Revision Booklet Preview
PPTX
How to Manage Global Discount in Odoo 18 POS
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
ACUTE NASOPHARYNGITIS. pptx
How to Manage Loyalty Points in Odoo 18 Sales
How to Manage Bill Control Policy in Odoo 18
Skill Development Program For Physiotherapy Students by SRY.pptx
Open Quiz Monsoon Mind Game Prelims.pptx
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
IMMUNIZATION PROGRAMME pptx
NOI Hackathon - Summer Edition - GreenThumber.pptx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Congenital Hypothyroidism pptx
Module 3: Health Systems Tutorial Slides S2 2025
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
The Final Stretch: How to Release a Game and Not Die in the Process.
High Ground Student Revision Booklet Preview
How to Manage Global Discount in Odoo 18 POS
Cardiovascular Pharmacology for pharmacy students.pptx
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Week 4 Term 3 Study Techniques revisited.pptx
Ad

JAVA Question : Programming Assignment

  • 2. 1) Write a Java Program to reverse a string without using String inbuilt function. Following program code explains this: public class FinalReverseWithoutUsingStringMethods { public static void main(String[] args) { // TODO Auto-generated method stub String str = "Automation"; StringBuilder str2 = new StringBuilder(); str2.append(str); str2 = str2.reverse(); // used string builder to reverse System.out.println(str2); } }
  • 3. 2) Write a Java Program to reverse a string without using String inbuilt function reverse() Answer: There are several ways with which you can reverse your string if you are allowed to use the other string inbuilt functions. In this method, we are initializing a string variable called str with the value of your given string. Then, we are converting that string into a character array with the toCharArray() function. Thereafter, we are using for loop to iterate between each character in reverse order and printing each character. public class FinalReverseWithoutUsingInbuiltFunction { public static void main(String[] args) { String str = "Saket Saurav"; char chars[] = str.toCharArray(); // converted to character array and printed in reverse order for(int i= chars.length-1; i&gt;=0; i--) { System.out.print(chars[i]); } } }
  • 4. 3) Write a Java Program to swap two numbers using the third variable. import java.util.Scanner; public class SwapTwoNumbers { public static void main(String[] args) { // TODO Auto-generated method stub int x, y, temp; System.out.println("Enter x and y"); Scanner in = new Scanner(System.in); x = in.nextInt(); y = in.nextInt(); System.out.println("Before Swapping" + x + y); temp = x; x = y; y = temp; System.out.println("After Swapping" + x + y); } }
  • 5. 4) Write a Java Program to swap two numbers without using the third variable. import java.util.Scanner; class SwapTwoNumberWithoutThirdVariable { public static void main(String args[]) { int x, y; System.out.println("Enter x and y"); Scanner in = new Scanner(System.in); x = in.nextInt(); y = in.nextInt(); System.out.println("Before Swappingnx = "+x+"ny = "+y); x = x + y; y = x - y; x = x - y; System.out.println("After Swapping without third variablenx = "+x+"ny = "+y); } }
  • 6. 5) Write a Java Program to count the number of words in a string using HashMap. import java.util.HashMap; public class FinalCountWords { public static void main(String[] args) { // TODO Auto-generated method stub String str = "This this is is done by Saket Saket"; String[] split = str.split(" "); HashMap<String,Integer> map = new HashMap<String,Integer>(); for (int i=0; i<split.length; i++) { if (map.containsKey(split[i])) { int count = map.get(split[i]); map.put(split[i], count+1); } else { map.put(split[i], 1); } } System.out.println(map); } }