So I have to design a program that takes a word and reverses the order of it. The exact directions are:Write a recursive method void reverse() that reverses a sentence. For example:
[code = Java]
Sentence greeting = new Sentence("Hello!");
greeting.reverse();
System.out.println(greeting.getText());
[/code]
prints the string "!olleH".
Implement a recursive solution by removing the first character, reversing a sentence consisting of the remaining text, and combining the two. So basically my biggest issue is my method to actually reverse the word.
public class Sentance
{
private String Word;
public Sentance (String aWord)
{
Word = aWord;
I am not very comfortable with Strings in Java. The problem I am facing is, taking a string that contains a sentence and reversing the words. Example would be, "Hi I am Bob" and changing it to "Bob am I Hi". Then returning the String.
My initial thoughts were to change the string into a character array and then manually doing the work with loops and tedious comparison statements. I quickly realized that there must be a better way but I am not very familiar with strings in Java to know what a more sufficient way would be.
how the code is getting all the permutations of a string with recursion. The following code works correctly but I am having trouble grasping what it is doing.
public class Main { private static void permutation(String prefix, String str){ int n = str.length(); if (n == 0) System.out.println(prefix); else { for (int i = 0; i < n; i++) permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1)); } } public static void main(String[] args) { permutation("", "ABCD"); } }
I see that each character is being appended to prefix through each iteration. So I know it adds "A" then "B" etc etc. I can follow it up until the "D" is appended to the prefix string. After that I don't understand how the D moves back to the str string and then the C moves back to str followed by the D moving back to the prefix string. I have stepped through the code many times in my debugger but still don't see how it moves back to str and knows to send D to prefix and not to send C. I feel I don't understand how recursion behaves therefore I can't follow.
How to calculate the minimum and the maximum in the same program.
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int integers; int[] numbers; numbers = new int[10]; int max; int min;
[Code] ....
My result when I input numbers are always the following:
"Numbers in reverse order are: 9, Numbers in reverse order are: 8, Numbers in reverse order are: 7, Numbers in reverse order are: 6 Numbers in reverse order are: 5, Numbers in reverse order are: 4, Numbers in reverse order are: 3, Numbers in reverse order are: 2, Numbers in reverse order are: 1, Numbers in reverse order are: 0."
This is a program to input a sentence and print the words that start with a vowel.......There is no syntax error but on executing and typing the sentence and pressing enter,I get a error saying :
java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:658) at sentence.main(sentence.java:22)
The program I'm working on is supposed to read input from a file and using recursion, print the pattern of asterisks for each value until the value is either < 0 or > 25. For example, if the value was 4, the pattern would look like this
* * * * * * * * * * * * * * * *
The values are stored in a file entitled prog3.dat which looks like this
4 3 15 26 0
I've never used recursion before and haven't been able to find anything showing how it would work with this particular type of problem. Here is what I've been able to come up with so far, but I'm having problems still which I will show following the code.
import java.util.Scanner; import java.io.*; public class Program3 { public static void main(String[] args) throws Exception { int num = 0; java.io.File file = new java.io.File("../instr/prog3.dat"); Scanner fin = new Scanner(file);
[Code] ....
Output:
Please enter an integer * * * * * * * * * * Please enter an integer * * * * * * Please enter an integer
As you can see, I don't know how to make it print the pattern like in the example and am honestly not even sure if this is recursion since I've never actually worked with recursion before.
I was asked to create a word pyramid program using recursion. I understand the concept of recursion (i.e. a method calling itself with the problem it is solving getting simpler and simpler each time) but I am having issues writing the program. Here is my code:
public static void main(String[] args) { //This program will create a word pyramid by using a recursive algorithm. //For example, the output of "DOGGY" should be: //DOGGY //OGG //G //This program will also use a recursive algorithm to accomplish this. String userWord = JOptionPane.showInputDialog(null, "Hello and welcome to the Word Pyramid program."
Recursion in a MineSweeper game. So far, when a user clicks on a square and it has a bomb on it, I reveal the bomb to the user - see clicked(). I also have the ability to show the user if the squares surrounding them have any adjacent bombs - see countAdjacentSquares().
However, I am having trouble with my fillZeros() recursive method. When a user clicks on a square that has zero bombs in its surrounding squares, it not only reveals that square, but also any adjacent squares (horizontally, vertically or diagonally adjacent) that also have zero bombs in its surrounding squares.
I don't have any recursion, if I click on a square with no bombs all it does it set the square to 0, but doesn't continue to check for other empty squares.
Java Code:
import javax.swing.*; import java.awt.*; import java.util.*; public class Square extends GameSquare { private boolean revealed = false; private boolean squareHasBomb = false; public static final int MINE_PROBABILITY = 10; public int mineCount = 0;
I am trying to sum up the elements of an array. When I test my code the sum is always off by one. For example if I input: 20, 40,30 it gives me 89 instead of 90.
This is what I have so far:
public static void main(String[args]){ int size = kbd.nextInt(); int [] myArray = new int [size] //user inputs the elements of array
I am writing a program to reverse a user inputted number (Example - 54321 is 12345) The code works great minus the fact that if I input a number that begins with zero, the output will drop the zero. Would it make more sense to just set this up using strings?
import java.util.*; public class Week7_Programming_Problem { static Scanner console = new Scanner(System.in); public static void main(String[] args) { int inputNum, outputNum;
My program is supposed to use recursion and iteration to sum and reverse elements in an array of Longs. The array is supposed to be 1000000 cells and I am supposed to compute the average times of 99999 trials. When I try to run the program, it keeps saying running.... but doesnt ever do anything.
public class PA2Delegate { private long[] start; private long[] end; private Long reversal; private Long sum = (long)(int)0;
I want to write an application that inputs a sentence, tokenizes the words with method split and reverse the words (not the letters)...I am stuck at the last part: how to reverse them...should I use the method reverse(); ?
Here is my code..
import java.util.Scanner; import java.util.StringTokenizer; public class ReversedWords { //execute application public static void main( String [] args) { //get sentence
To delete "the" from string and display the new string my input------ the dog required output--------- dog my output-------------------thedog
Code :
import java.util.*; class the { public static void main(String args[]) { Scanner in=new Scanner(System.in); System.out.println("Enter a sentence"); String s=in.nextLine();
I am trying to write a simple program that checks if a user's input has a specific letter using the ".contains" method but its not doing what i wanted to do. here is my code below.
import java.util.Scanner; public class secret { public static void main(String args[]) { Scanner sc; char hidden='a'; String guess;
i am new to programming skills it may be silly question for experience but for me it's new thing. actually i tried a lot but i am facing problem when i trying to take input through Scanner. if i will take input a sentence directly as a string it's working . but when i am trying with Scanner the first word is showing next are not showing
public class Demo2 { public static void main(String[] args) { String s1="hi how are you"; s1=s1.replace('a', 'A'); s1 =s1.replace('e', 'E'); s1 =s1.replace('i', 'I');
[Code]...
this is working properly.
but when i trying with Scanner i am facing problem.
public class Demo2 { public static void main(String[] args) { java.util.Scanner scn= new java.util.Scanner(System.in) String s1=scn.next(); s1=s1.replace('a', 'A');
Ok, so I have to make a program that tells the user to type a sentence and at the end of the sentence a "." must be put to end the program. If a "." period is not entered, it must re-prompt the user to enter a period. On top of all this, the program has to count the number of letters in the sentence. I cant seem to get the while loop working and I cant find anywhere how to make a counter count the number of letters.
import java.io.*; class Sentence //The purpose of this program is to
The purpose of this program is to translate a user inputed sentence into morseCode. It seems like everything is right to me, but I'm simply not getting output! What am I doing wrong, or what should I add/change?
Here is the main class:
public class MorseCode { public static String myInput; public static String[] myMorse; public static String myUserInput; public static char[] myAlph = {'A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; public MorseCode()
My assignment is to write a program that will encrypt and decrypt a sentence entered by a user but the encryption is to be random using an array. Can I convert my sentence(string) from char to int then create a random array to encrypt?
import java.util.Scanner; import java.util.Random; /*SentenceEncryptionProgram */ public class SentenceEncryption { string sentence; //sentence entered by user
I've been given an assignment for my Programming Fundamentals class that requires the user to enter a word/sentence to encrypt or decipher, as well as requiring the user to enter a key in which to encrpyt/decrypt the input by.
So a = no shift, b = a shift by 1 and so on.
But I'm completely lost as to what to do, and after searching for Vigenere Cipher topics, they look completely different to mine and (hardly) include prompts?This is my code:
/** * @(#)VigenereCipher.java */ import java.util.Scanner; public class VigenereCipher { public VigenereCipher()