SInce I am a NEWB, I figured I'd create this thread for issues I or others may have relating to coding.
My current courseork involves loops in Java. The story of my life is such that once I change something I get hit with a roadblock.
"You want to be a programmer?"
"I do!"
"Well, here's a assignment that will drive you nucking futs."
Alright. My task is to use a single while loop to print out even and odd numbers between 50 and 100. The even numbers will be labled and printed on the same line while the odd numbers will follow the same format. For example:
Even numbers between 0 and 50: 50, 52, 54, 56, 58, ... , 100
Odd numbers between 0 and 50: 51, 53, 55, 57, 59, ... , 99
I have nailed the code to display the numbers on the same line but it only works properly when I disable one portion of the loop. If I leave all of the code intact, the output is as follows:
public static void main(String args[])
{
//start at 50
int evenNumber = 50;
int oddNumber = 50;
//execute the commands when both the even and odd numbers are less than or equal to 100
while ((evenNumber <= 100) && (oddNumber <= 100))
{
if (evenNumber % 2 == 0)
{
System.out.print(evenNumber + ", ");
}
evenNumber++;
//if I disable the if statement for the odd number, everything works fine. The same can be said if I disable the if statement for
//the even number and leave hte code for the odd
if (oddNumber % 2 != 0)
{
System.out.println();
System.out.print(oddNumber + ", ");
}
oddNumber++;
}
}
Now, I thought about storing the values in a string and just printing those, but after many attempts I couldn't get it to work. Any advice? Please do not offer code because I won't learn that way.
Re: Dumping Ground for Coding Issues
Posted: Thu Mar 05, 2015 6:29 am
by Flagg
Kill yourself.
Re: Dumping Ground for Coding Issues
Posted: Thu Mar 05, 2015 4:17 pm
by adr
Yeah, storing the things in a string is what you want to do. Instead of printing it, just do a += on the string to add the portion to it. Then, after the loop, print both strings.
There's a trick you can do with the commas btw, but I'll let you play for a bit before saying it.
public class EvenOdd
{
public static void main(String args[]) {
//start at 50
int number = 50;
//declare strings to hold the even and odd numbers
String evenNumbers = "";
String oddNumbers = "";
//execute the commands when both the even and odd numbers are between 50 and 100
while (number <= 100)
{
if (number % 2 == 0)
{
//when the even number is 100, remove the comma after the number. this makes the output prettier
if (number == 100)
{
//assign 100 to a string
evenNumbers = evenNumbers + "" + number;
}
else
{
//assign even numbers to a String
evenNumbers = evenNumbers + "" + number + ", ";
}
}
else
{
//when the odd number is 99, remove the comma after the number. this makes the output prettier
if (number == 99)
{
//assign 99 to a string
oddNumbers = oddNumbers + "" + number;
}
else
{
//assign odd numbers to a String
oddNumbers = oddNumbers + "" + number + ", ";
}
}
//continue counting
number++;
}
//print the result to the user by getting the length of the strings holding the even and odd numbers
evenNumbers = evenNumbers.substring(0, evenNumbers.length());
oddNumbers = oddNumbers.substring(0, oddNumbers.length());
System.out.println("Even numbers between 50 and 100 : " + evenNumbers);
System.out.println("Odd numbers between 50 and 100 : " + oddNumbers);
}
}
Now it's time to do this with methods, which is how every program in the future should be written in the class. I've been using methods since the second week of class, so it will be a no brainer.
The tip with the commas btw: it is natural to think of putting them after a number since that's how it looks.... but there's another way to think of it which is simpler to code.
A comma (and space, the string is ", "), goes before all items in the list, except the first one.
Consider that for a bit and see if you can change the code a bit to incorporate it (the spoiler is just the other way I think of it, it isn't the final answer, so feel free to look if you want). Then I'll show you how I'd write it too later today.
Everything works as its supposed to. Hmm. I didn't see that coming but now I know why.
Re: Dumping Ground for Coding Issues
Posted: Wed Mar 18, 2015 4:51 am
by Agent Bert Macklin
I;m not having any issues, but I just wanted to pop in and say that I am in lvoe with using OSX's terminal for coding. I got my hands on Sublime Text to use as an editor. Coding this way is not as efficient as using my go-to IDE IntelliJ, but it's still pretty fun.
Re: Dumping Ground for Coding Issues
Posted: Sat Sep 19, 2015 3:47 am
by Agent Bert Macklin
I'm not having any coding issues, but I just wanted to pop in and say that what I'm learning in Java at this moment in class is far more interesting than the basics. We've covered the basics of OOP and are currently learning about exception handling and reading and writing files. It's pretty great.
I've also decided that my first personal project will be an application that allows camera owners to get a variety of information about their cameras. For instance, shutter count, battery life, copyright info, etc. I am doing this because my camera brand of choice (Canon) does not have shutter count information in the EXIF data. I have to resort to online applications or standalone applications. It's a pain in the cock, for sure.
Re: Dumping Ground for Coding Issues
Posted: Mon Sep 21, 2015 12:35 pm
by adr
OOP was overrated for a while, called the best thing ever that solves all problems (around the time Java was created...), but nowadays I think it is underrated. Sure, it doesn't solve everything, but it does solve a bunch of things.
Exceptions just kinda rock, I love them.
Are you going into more theory behind OOP or just the basics of use? Two interesting things about it I like is for one, the Liskov substitution principle reminding you when to use inheritance (if you can't substitute child class for parent, it generally isn't appropriate to inherit) and just the interesting realization that comes off that: the most code reuse from OOP does *not* come from methods being inherited to base classes like a lot of newbies assume, but actually from other functions taking the base class. *Those* functions get to be reused for your children because you can substitute them for the parent and still call it.
Re: Dumping Ground for Coding Issues
Posted: Tue Sep 29, 2015 2:15 am
by Agent Bert Macklin
I think we'll be doing more than the basics for OOP. We've talked about parent classes, child classes, and are currently on abstract classes. I've not yet looked ahead in the book to see what else is covered, but I do know that I will need to learn more on my own outside of class if I am to be successful in this field.
Re: Dumping Ground for Coding Issues
Posted: Sun Oct 25, 2015 8:52 pm
by Agent Bert Macklin
GUI in Java is pretty cool. I like how setting things up properly is reminiscent of a stage play. It's awesome. Pain in the ass to type all of that stuff, but awesome nonetheless.
Re: Dumping Ground for Coding Issues
Posted: Thu Jan 28, 2016 4:12 am
by Agent Bert Macklin
Rant: WHy the hell does my school teach Java for two classes and then switch to C++ for data structures? It's difficult going from a newer language to an older. I'm getting the hang of OOP with C++ and classes, but it's difficult.
I willl say this: it feels like I am coding when I'm using C++.
Re: Dumping Ground for Coding Issues
Posted: Thu Jan 28, 2016 6:30 pm
by adr
C++ was started before Java, but both are still actively changing so modern C++ isn't really much older than modern Java....
But the reason is probably that C++ offers more capabilities to really get into the structures' details than Java does. A lot of schools make that transition.