| |||||||
| Coding / Scripting / Programming Discussions on all manner of coding and scripting. |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 (permalink) | |
| Lokie's Personal WU-Hoe | For the current year, I am enrolled in "AP Computer Science" which has turned out to be completely Java oriented and moves at a blistering pace. I've never programmed before in my whole life, so everything we learn is a completely new concept and it is rather hard to grasp everything right away. Yes, this is homework, so I am not asking for the finished code from anyone, just rather point me in the right direction and tell me EXACTLY what tags I need. In other words, I'm looking for pieces of a puzzle all messed up in a box, not put together. Right now it just feels like a bunch of pieces are missing and it's up to me to create them. What I'm looking for is some help with projects that I can't figure out. Usually I get to the point where no matter what I try, getting further is almost impossible since I simply don't have a clue what to do. If there turns out to be some people that can help, I will just post any more programs I need help with in this thread(and if anyone else is taking Java classes, feel free to post your questions here too). Here's the current one I need help on, it's called "Palindrome Tester". What I need to do is incorporate a function that eliminates the spaces and punctuation from what the user entered, and just read the characters of that string. Example: "L.;; O ....; :: L ::,, " = "LOL". I've already taken care of the character case issue by the way. Here's the code so far: Code:
import cs1.Keyboard;
public class Pally
{
public static void main (String[] args)
{
String str, another = "y";
String casemutation; // Standardizes the input to all the same case
String puncmutation; // Removes all the spaces and punctuation
int left, right;
while (another.equalsIgnoreCase("y")) // allows user to say yes or no
{
System.out.println ("Enter a potent[ial] palindrome: ");
str = Keyboard.readString();
casemutation = str.toUpperCase();
str = casemutation;
left = 0;
right = str.length() - 1;
while (str.charAt(left) == str.charAt(right) && left < right)
{
left++;
right--;
}
System.out.println();
if (left < right)
System.out.println ("That string is NOT a pally.");
else
System.out.println ("That string IS IN FACT a pally.");
System.out.println();
System.out.print ("Test another pally ([Y] or [N]?");
another = Keyboard.readString();
}
}
} | |
| | | |
| Sponsored Links |
| | #2 (permalink) | ||||||||||||||||||||||||
I feel sorry for you, I hate code. Hope you get it sorted. | |||||||||||||||||||||||||
| | | ||||||||||||||||||||||||
| | #3 (permalink) | |
| Lokie's Personal WU-Hoe | Yeah, coding is not my cup of tea. Hardware is where my heart's at, not some silly commands on a screen. The reason I took this class was to learn about this different world at school instead of my own time, plus it's always better to be a well rounded person with computer knowledge. | |
| | | |
| | #4 (permalink) | ||||||||||||||||||||||||
If I understand your problem correctly you're looking to filter out any crap that the user enters . The method to determine if a character is a letter and not some crap is Character.isLetter(). Knowing this you just loop through your string and test each letter like so: Code: String inputstring , outputstring = "";
for(int ctr=0;ctr < inputstring.length();ctr++) //loop through the input string
{
if( Character.isLetter( inputstring.charAt(ctr) ) ) //test if the character at position ctr is a letter
outputstring += inputstring.charAt(ctr); //if it is add it to the output string.
} Hope it helps!!! Last edited by Morion; 21-October-05 at 08:41 AM. | |||||||||||||||||||||||||
| | | ||||||||||||||||||||||||
| | #7 (permalink) | ||||||||||||||||||||||||
NP glad to help out. Also, I recommend signing up for the forums over at gamedev.net. It's a game programming website but the people in the forums are very helpful and wicked smart. Great for when that important program is due but it just won't work. http://www.gamedev.net/community/forums/ | |||||||||||||||||||||||||
| | | ||||||||||||||||||||||||
| | #8 (permalink) | |
| Lokie's Personal WU-Hoe | I just can't seem to figure this out after trying for an hour. What do I need to do to make the String AMPM work with the output? What needs to happen is any hour 12 or after entered must be declared as PM, while any hour 0-11 needs to be AM. I added an if else statement for the String but it keeps giving me a compiler error every time it's added to the output. Any help at all? Chances are I'll be able to get some help tomorrow, but in the mean time it'd good to have someone else take a look at this. Code: /**
* The ClockDisplay class implements a digital clock display for a
* European-style 24 hour clock. The clock shows hours and minutes. The
* range of the clock is 00:00 (midnight) to 23:59 (one minute before
* midnight).
*
* The clock display receives "ticks" (via the timeTick method) every minute
* and reacts by incrementing the display. This is done in the usual clock
* fashion: the hour increments when the minutes roll over to zero.
*
* @author Michael Kolling and David J. Barnes
* @version 2001.05.26
*/
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString; // simulates the actual display
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at 00:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
}
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at the time specified by the
* parameters.
*/
public ClockDisplay(int hour, int minute)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour, minute);
}
/**
* This method should get called once every minute - it makes
* the clock display go one minute forward.
*/
public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
updateDisplay();
}
/**
* Set the time of the display to the specified hour and
* minute.
*/
public void setTime(int hour, int minute)
{
String AMPM; // this is the string that is supposed to add AM or PM to the readout
if (1 >= hour%12)
AMPM = "PM";
else
AMPM = "AM";
if (hour == 24)
hour = 12;
else if (hour > 12) // changes the clock to 12 hours now using a remainder
hour = hour%12;
hours.setValue(hour);
minutes.setValue(minute);
updateDisplay();
}
/**
* Return the current time of this display in the format HH:MM.
*/
public String getTime()
{
return displayString;
}
/**
* Update the internal string that represents the display.
*/
private void updateDisplay()
{
displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue() +" "+AMPM;
//after +AMPM is added, it doesn't work(cannot find symbol - variable AMPM.
}
} | |
| | | |
| | #9 (permalink) | ||||||||||||||||||||||||
Have you learned about scope yet? The string variable AMPM is declared in the member function setTime() and only exists in memory as long as that function is executing and is only accesible inside that function. Thats why you get the error. Once it terminates the variable goes out of scope and java marks it for garbage collection. If you declare AMPM at class level then all member functions will be able to see it and it will exsist in memory as long as the class does. Like so: Code: public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString; // simulates the actual display
private String AMPM; //declare it here
//All the rest of the code.......... | |||||||||||||||||||||||||
| | | ||||||||||||||||||||||||
| | #11 (permalink) | ||||||||||||||||||||||||
Yeah I think many of these java courses use the same material. I week after i first answered this post I looked through my textbook and saw the exact same palindrome program that Kage was asking about. Plus we also had to make a time class. | |||||||||||||||||||||||||
| | | ||||||||||||||||||||||||
| | #12 (permalink) | |
| Lokie's Personal WU-Hoe | Hey, thanks again for helping Morion. That quick reply is greatly appreciated I think we learned about scope, but just didn't do anything with it and instead just had a brief overview. One minor problem with this program now is that when the "Tick" button is clicked and it goes from 11:59 AM to 12:00, it will still display 12:00AM. Although when 12:00 is entered, it will display 12:00PM like I want it to.Cpt. Planet, I will most definitely take you up on that offer. Just as long as you don't mind helping that is, I don't want to become annoying or anything. | |
| | | |
| | #13 (permalink) | ||||||||||||||||||||||||
It's probably because when you manually entered the time it goes through the setTime() function which has all the checks to see if it's AM or PM: eg. Code: if (1 >= hour%12)
AMPM = "PM";
else
AMPM = "AM"; | |||||||||||||||||||||||||
| | | ||||||||||||||||||||||||
| | #14 (permalink) | |
| Lokie's Personal WU-Hoe | OK, back after a while. I have been able to muster through most of these projects lately, except this last one has me on a time crunch because of trailing behind and taking too long on the others. It's this "Card & DeckOfCards" project where we are using the ArrayList class to do some objectives with the Card class. I have commented everything that needs to be done and filled in what I wrote down in class that he gave us for a jump start. What would be of great help is if you could write some pseudo code to help me along, right now I am at a loss of ideas of what to do. All progress right here: Class for the action methods: Code: public class DeckOfCards
{
// Store 52 objects of the Card class
private Arraylist deck = ???;
public DeckOfCards
{
Arraylist deck = ???;
for (int face = 1; face <= 13; face++
for (int suit = 1; suit <= 4; suit++)
deck.add (new Card (face, suit));
}
// Shuffle the deck
// Deal a card
// Report number of cards left in the deck
public void printDeck
{
}
} Driver Class to use all the methods: Code: public class DriverDealCard
{
// Driver Class with main method that deals each card
// from a shuffled deck, printing each card as it is
// dealt.
public static void main (String[] args)
{
public DeckOfCards myDeck = new DeckOfCards;
myDeck.printDeck();
myDeck.shuffle();
}
} Any guidance is appreciated -Kage | |
| | | |
| | #15 (permalink) | |
| Good, luck, it's been a while since I've done Java, so I just went through my saved files because I swear I did something with cards, but came up empty. If you need to make a yahtzee program however, I have a killer one with all the bells and whistles I can reference. I also did a project with a randomly generated maze that the computer solves. Lately, I've been doing more COBOL, so if you need help there, drop me a line, I'll be watching this thread. Also check here for some help: http://java.sun.com/j2se/ and http://forum.java.sun.com/thread.jsp...sageID=4021781 | ||
| | | |
| | #16 (permalink) | |
| IF USING JAVA 1.5 (TIGER) ONLY OK, it wont add the generics feature so do this for every ArrayList for Java 1.5 ArrayList(greater-than sign)Card(less-than sign) Code: ArrayList Code: ArrayList deck=new ArrayList();
For shuffling, try this
for(int y=0;y<5;y++)
{
ArrayList firstHalf=new ArrayList();
ArrayList secondHalf=new ArrayList();
for(int z=0;z<26;z++)
firstHalf.add(deck.get(z));
for(int a=26;a<52;a++)
secondHalf.add(deck.get(a));
deck=new ArrayList();
// combines the 2 halves
for(int x=0;x<26;x++)
{
deck.add(secondHalf.get(x));
deck.add(firstHalf.get(x));
}
// This for loop swaps 13 pairs of selected cards positions in the deck
for(int t=0;t<13;t++)
{
Card obj;
Card obj2;
int index,idex2; // stores indices of the cards to swap
Random switchCard=new Random (); //used to select what cards to swap
index=switchCard.nextInt(52);
for(int wait=0;wait<25;++) //waits 25ms before selecting new Random int
index2=switchCar.nextInt(52);
obj=(Card)deck.get(index);
obj2=(Card)deck.get(index2);
//switches the 2 cards positions in the deck
deck.set(index,obj2);
deck.set(index2,obj);
}
}
public String dealCard()
{
Card card=(Card)deck.remove(0); //removes the top card of the deck;
String str="Dealt a card: "+Card.getFace()+" "+Card.getSuit();
return str;
} Last edited by The Captain; 29-January-06 at 10:55 PM. | ||
| | | |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How do you take your java? | Anchi | Anything Goes | 19 | 25-March-05 08:07 AM |
| Slashdot // Quest For "Unbreakable Java" Unites ABAP & Java | Gizmo | Slashdot RSS | 0 | 03-January-05 02:37 PM |
| HELP! Need JAVA Pro! | DuckWarrior | Graphics / Media | 16 | 26-June-04 01:40 PM |
| Java Help | Big Deuce | Anything Goes | 11 | 26-January-04 02:13 PM |
| Java Help | Joose | Coding / Scripting / Programming | 3 | 23-January-04 09:34 AM |