Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,561 members, 7,809,050 topics. Date: Thursday, 25 April 2024 at 09:52 PM

Help Java Programmers: Need Help With A Java Assignment - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help Java Programmers: Need Help With A Java Assignment (3205 Views)

How Long Did It Take You Guys To Become Strong Java Programmers / Java Proramers:java Assignment: Can Someone Help Provide A Solution To This (2) (3) (4)

(1) (Reply)

Help Java Programmers: Need Help With A Java Assignment by buster(m): 6:39pm On Dec 07, 2014
Hi everyone

Av got this java assignment i have been battling with but i just cannot get a straight way to write the code. Java really aint my thing but its part of requirement for the program am running and have got till Tuesday noon to submit. Kindly find assignment written below:

Programming Assignment

For this week’s assignment, you will write a program class that has two subroutines and a main routine. The program should be a part of the ‘firstsubroutines’ class and you should name your project firstsubroutines if you are using Netbeans.

Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome.

To determine whether a string is a palindrome, you can: convert the string to lower case; remove any non-letter characters from the string; and compare the resulting string with the reverse of the same string. If they are equal, then the original string is considered to be a palindrome.

Here’s the code for computing the reverse of str:

String reverse;

int i;

reverse = "";

for (i = str.length() - 1; i >= 0; i--) {

reverse = reverse + str.charAt(i);

}

As part of your assignment, you must write a static subroutine that finds the reverse of a string. The subroutine should have one parameter of type String and a return value of type String.

You must also write a second subroutine that takes a String as a parameter and returns a String. This subroutine should make a new string that is a copy of the parameter string, except that all the non-letters have been omitted. The new string should be returned as the value of the subroutine. You can tell that a character, ch is a lower-case letter by testing if (ch >= 'a' && ch <= 'z')

(Note that for the operation of converting str to lower case, you can simply use the built-in toLowerCase

subroutine by saying: str = str.toLowerCase()wink

You should write a descriptive comment before each subroutine, saying what it does.

Finally, write a main() routine that will read in a string from the user and determine whether or not it is a palindrome. The main routine should use The program should print the string converted to lower case and stripped of any non-letter characters. Then it should print the reverse string. Finally, it should say whether the string is a palindrome. (Use the two subroutines to process the user's string.) For example, if the user's input is "Hello World!", then the output might be:

stripped: helloworld

reversed: dlrowolleh

This is NOT a palindrome.

and if the input is "Campus motto: Bottoms up, Mac!", the output might be:

stripped: campusmottobottomsupmac

reversed: campusmottobottomsupmac

This IS a palindrome.

You must complete your program, test, debug, and execute it. You should test two different cases, one that is a palindrome and another one that is not a palindrome. You must submit your java code file (preferably by cut and paste the code into the assignment dialog box). The output of your program must be captured by either copying the content in the output window and pasting it into the assignment dialog box or by capturing the image of the screen which contains the output of your java program or the output and submitting this with your assignment as an attachment. In windows you can capture a screen shot with the Ctrl Alt and Print Screen key sequence. This image can then be pasted into a Word, WordPad, or OpenOffice document which can be submitted with your assignment.
Re: Help Java Programmers: Need Help With A Java Assignment by nollyj: 10:48pm On Dec 07, 2014
This is your assignment. You have to at least try and show that you have done something.

You can get an idea from this code. I did not test it but hopefully it may work.



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package firstsubroutines;

import java.util.Scanner;


public class Firstsubroutines {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

Scanner input = new Scanner(System.in);
String userInput = input.nextLine();

String formatString = removeNonCharacters(userInput);
System.out.println("Stripped: " + formatString);

String reversedString = reverseStringInput(formatString);
System.out.println("Reversed: " + reversedString);

if (formatString.equals(reversedString)) {
System.out.println("This is a palindrome" );
} else {
System.out.println("This not a palindrome" );
}
}

private static String reverseStringInput(String userInput) {

String reverse = "";
for (int i = userInput.length() - 1; i >= 0; i--) {
reverse = reverse + userInput.charAt(i);
}
return reverse;
}

private static String removeNonCharacters(String userInput) {

String userInputLowerCase = userInput.toLowerCase();
String formattedUserInput = "";

for (int i = 0; i < userInputLowerCase.length(); i++) {

char charValue = userInputLowerCase.charAt(i);

if (charValue >= 'a' && charValue <= 'z') {
formattedUserInput += userInputLowerCase.charAt(i);
}
}
return formattedUserInput;
}
}


Re: Help Java Programmers: Need Help With A Java Assignment by YourCrush: 4:38pm On Dec 10, 2014
package work;

/**
*
* @author YourCrush
*/
public class Assignment {


private static String wordReverser(String inputword){

String wr;
StringBuilder sb=new StringBuilder();

for(int i=inputword.length(); i>0; i--){
sb.append(inputword.substring(i-1,i));
}
wr=sb.toString();

return wr;
}


private static String nonAlphabetStripper(String inputword){

char alphabet='a';
String word;
StringBuilder sb=new StringBuilder();
int k;

for(int j=0; j<inputword.length(); j++){
String LettersofWord=inputword.substring(0+j,1+j);

for(k=0; k<26; k++){
char letter= alphabet++;
String s=String.valueOf(letter);

if(LettersofWord.equalsIgnoreCase(s)){
sb=sb.append(LettersofWord);
}
}
k=0;
alphabet='a';

}
word=sb.toString();

return word;
}


private static void palindromeDetector(String strip, String reverse){

if(strip.equalsIgnoreCase(reverse)){
System.out.println("This is palindrome"wink;
}else{
System.out.println("This is not palindrome"wink;
}

}


public static void main(String [] args){

String userinputword="race^!!24%%%---car"; //You may assign any value of choice to VARIABLE userinputword.

String strip=nonAlphabetStripper(userinputword);
System.out.println("Stripped: "+strip);

String reverse=wordReverser(strip);
System.out.println("Reversed: "+reverse);

palindromeDetector(strip, reverse);

}

}

(1) (Reply)

How To Make Copyright Symbol In Vb.net / I Just Wrote What Might Be The Worst System Ever Programmed In This Country / Has Anyone Tried Out Windows 10? Pls Share Your Experience

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 32
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.