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

Recently uploaded (20)

PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 

Featured (20)

PDF
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
 
PDF
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
 
PDF
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
 
PDF
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
 
PDF
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
PDF
Everything You Need To Know About ChatGPT
Expeed Software
 
PDF
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
PDF
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
PDF
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
PDF
Skeleton Culture Code
Skeleton Technologies
 
PDF
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
PDF
Content Methodology: A Best Practices Report (Webinar)
contently
 
PPTX
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
PDF
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
PDF
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
PDF
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
PDF
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
PDF
Getting into the tech field. what next
Tessa Mero
 
PDF
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
PDF
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
 
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
 
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
 
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
 
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
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); } }