Featured Worklog

Price Search



PC Apex Sponsor


PC Apex Sponsors



PC Apex RSS Feeds

RSS Feed for PC Apex Reviews & ArticlesRSS Feed for PC Apex PC Modding WorklogsRSS Feed for the PC Apex Daily DisturbanceRSS Feed for the latest PC Apex Site NewsRSS Feed for PC Apex Affiliate and Web NewsRSS Feed for PC Apex Deals and Steals

Go Back   Apex Community Forums // Other Forums // Designers Corner // Coding / Scripting / Programming

Coding / Scripting / Programming Discussions on all manner of coding and scripting.

Reply
 
LinkBack Thread Tools Display Modes
Old 18-October-05, 08:37 PM   #1 (permalink)
Lokie's Personal WU-Hoe
Default Can anyone help me out with Java?

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();
        }
    }
}
Kage is offline     Reply With Quote
Sponsored Links
Old 19-October-05, 04:47 AM   #2 (permalink)
Sempr0n?
reflux's Avatar
Default

Quote:
Originally Posted by Kage
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.

I feel sorry for you, I hate code. Hope you get it sorted.
reflux is offline     Reply With Quote
Old 20-October-05, 09:48 PM   #3 (permalink)
Lokie's Personal WU-Hoe
Default

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.
Kage is offline     Reply With Quote
Old 20-October-05, 10:53 PM   #4 (permalink)
Apex Master Tech
Morion's Avatar
Default

Quote:
Originally Posted by Kage
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.


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.
 }
Coding can be very frustrating but once you develop a deep understanding of it it is very rewarding anf you can do some really cool stuff.

Hope it helps!!!

Last edited by Morion; 21-October-05 at 08:41 AM.
Morion is online now     Reply With Quote
Old 20-October-05, 11:04 PM   #5 (permalink)
Apex Tech Demi-God
subdismal's Avatar
Default

Damn, I absolutely hate coding anything but Web languages. I did however take home a 2nd place award in an FBLA district competition in my high school for Java Programming. Too bad I entered as a joke and there were only two entries in my district.

subdismal is offline     Reply With Quote
Old 21-October-05, 08:34 PM   #6 (permalink)
Lokie's Personal WU-Hoe
Default

Thank you much Morion for helping me out, that's exactly what I wanted to do but had no idea how to do it. Knowing that Character.isLetter tag really helps out
Kage is offline     Reply With Quote
Old 21-October-05, 09:12 PM   #7 (permalink)
Apex Master Tech
Morion's Avatar
Default

Quote:
Originally Posted by Kage
Thank you much Morion for helping me out, that's exactly what I wanted to do but had no idea how to do it. Knowing that Character.isLetter tag really helps out

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/
Morion is online now     Reply With Quote
Old 08-November-05, 09:05 PM   #8 (permalink)
Lokie's Personal WU-Hoe
Default

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.
    }
}
Kage is offline     Reply With Quote
Old 09-November-05, 09:33 AM   #9 (permalink)
Apex Master Tech
Morion's Avatar
Default

Quote:
Originally Posted by Kage
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.
    }
}

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..........
Morion is online now     Reply With Quote
Old 09-November-05, 11:11 AM   #10 (permalink)
That's Mr. Freeze to you!
Cpt.Planet's Avatar
Default

Wow man I did that same exact program in my programming class about a month ago. The only thing I did different was it was a 24 hour clock. If you ever need any help I might be able to help out.
Cpt.Planet is offline     Reply With Quote
Old 09-November-05, 11:48 AM   #11 (permalink)
Apex Master Tech
Morion's Avatar
Default

Quote:
Originally Posted by Cpt.Planet
Wow man I did that same exact program in my programming class about a month ago. The only thing I did different was it was a 24 hour clock. If you ever need any help I might be able to help out.

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.
Morion is online now     Reply With Quote
Old 09-November-05, 10:25 PM   #12 (permalink)
Lokie's Personal WU-Hoe
Default

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.
Kage is offline     Reply With Quote
Old 09-November-05, 10:49 PM   #13 (permalink)
Apex Master Tech
Morion's Avatar
Default

Quote:
Originally Posted by Kage
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.

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";
but when your tick function executes and the hours must be increased it calls your hours.increment() function and if those if statements in the code block above aren't in there then it will just increase the hour and leave your AMPM string alone.
Morion is online now     Reply With Quote
Old 29-January-06, 09:01 PM   #14 (permalink)
Lokie's Personal WU-Hoe
Lightbulb

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();
	    
	}
}
Big Mama Card class is attached via Text Doc if you need it.

Any guidance is appreciated -Kage
Attached Files
File Type: txt CardClass(Huge).txt (5.2 KB, 72 views)
Kage is offline     Reply With Quote
Old 29-January-06, 09:42 PM   #15 (permalink)
Apex Tech Maniac Supreme
Peng Lord's Avatar
Default

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
Peng Lord is offline     Reply With Quote
Old 29-January-06, 10:19 PM   #16 (permalink)
Apex Master Tech
The Captain's Avatar
Default

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 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,index2; // 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=deck.get(index);
obj2=deck.get(index2);
//switches the 2 cards positions in the deck
deck.set(index,obj2);
deck.set(index2,obj);
}
}
public String dealCard()
{
Card card=deck.remove(0); //removes the top card of the deck;
String str="Dealt a card: "+Card.getFace()+" "+Card.getSuit();
return str;
}
IF YOU ARE NOT USING JAVA 1.5 (Tiger) ONLY
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.
The Captain is offline     Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT -5. The time now is 08:42 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC5
Copyright PCApex.com, GameApex.com, ForumApex.com 2001 - 2008
Advertisements