View Full Version : Java HW ;_;
CrispyChicken
10-15-2014, 09:52 PM
Since my textbook is useless and doesn't help me learn, I've come to my fellow nerds to help me with some Java code.
Problem 1
http://www.retrouprising.com/img/2014_10/4753.jpg
Problem 2
http://www.retrouprising.com/img/2014_10/4754.jpg
I've gotten pretty much everything else so far, but I've been stuck on these two problems (and two others) for an hour so far. I'm probably overthinking it/tired, but I want to get my work done.
Tails
10-15-2014, 10:12 PM
I'd fail this class right away.
CrispyChicken
10-15-2014, 10:17 PM
I'd fail this class right away.
So far I've done pretty well. I have assignments in this eBook, but most of the time it does a bad job at explaining the topic. So it's trial and error until I get it right.
Seahawk
10-15-2014, 10:53 PM
Problem 1:
String API (http://docs.oracle.com/javase/8/docs/api/java/lang/String.html)
Character API (http://docs.oracle.com/javase/8/docs/api/java/lang/Character.html)
Iterate through each character in the string, testing for if it is a digit.
for(int i = 0; i < 3; i++)
{
char c = passCode.charAt(i);
if(c.isDigit())
hasDigit = true;
}
---------- Post added at 11:53 PM ---------- Previous post was at 11:47 PM ----------
Problem 2:
String API (http://docs.oracle.com/javase/8/docs/api/java/lang/String.html)
Character API (http://docs.oracle.com/javase/8/docs/api/java/lang/Character.html)
Iterate through each character in the string, checking for whitespace.
for(int i = 0; i < 2; i++)
{
char c = passCode.charAt(i);
if(c == " ")
System.out.println("Whitespace detected at character position " + i + ".");
}
CrispyChicken
10-15-2014, 11:28 PM
Problem 1:
I already tried that. It passes the first test, but the system's second test fails it.
http://www.retrouprising.com/img/2014_10/4755.png
Problem 2:
Second one says the "==" is an error, reason being "incomparable types: char and java.lang.String"
This eBook is annoying. I feel like it works so differently than Eclipse.
---------- Post added at 12:28 AM ---------- Previous post was at 12:26 AM ----------
This things not due until Monday and I'm pretty tired (got class early tomorrow) so I gotta crash. I'll check back her tomorrow afternoon when I get home.
Seahawk
10-16-2014, 11:55 AM
Problem 1:
Your code is different. See bolded.
My test:
for(int i = 0; i < 3; i++)
{
char c = passCode.charAt(i);
if(c.isDigit())
hasDigit = true;
}Your test:
for(int i = 0; i < 3; i++)
{
char c = passCode.charAt(i);
if(Character.isDigit(i))
hasDigit = true;
}
You are not testing the character c, you are calling the isDigit(int codePoint) (http://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isDigit-int-) function from the Character class and passing it the current loop index as the parameter. This is completely different from what you want to do.
Problem 2:
Replace the " " (quotation marks) with ' ' (apostrophes).
---------- Post added at 12:55 PM ---------- Previous post was at 01:14 AM ----------
Ok so never code late at night...mine was wrong. Yours was correct but you were still passing the array index instead of the character itself.
Problem 1:
for(int i = 0; i < 3; i++)
{
char c = passCode.charAt(i);
if(Character.isDigit(c))
hasDigit = true;
}
Problem 2:
Replace the " " (quotation marks) with ' ' (apostrophes).
Alternatively, use the isWhitespace(char ch) (http://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isWhitespace-char-) function.
for(int i = 0; i < 2; i++)
{
char c = passCode.charAt(i);
if(Character.isWhitespace(c))
System.out.println("Whitespace at position " + i + ".");
}
CrispyChicken
10-16-2014, 05:43 PM
Thanks for the help Seahawk, both are right now.
I was close on the first one, needed the 'c' instead of the 'i' :p
And second one was an easier fix, just needed to replace the " " with ' '
---------- Post added at 06:43 PM ---------- Previous post was at 04:52 PM ----------
Write a for loop in Java that prints the integer values from 1 through n, counting by inc. For example,
if n is 25 and inc is 5, the loop will print the sequence
1
6
11
16
21
Your code should work for any positive values of n and inc. You may assume that the integer variables
n and inc have already been declared and assigned values. Just write the code for the loop!
Any help with this one? I know what the question is asking me, I just don't know how to get the counter to work/stop.
Google: Java For Loop
First link
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics) (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html)
Example from that page.
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
Edit for your needs.
Answer in spoiler but not much of a point in just copying it. Learning to find and use examples (steps above) is the most important lesson. Step below (answer) is meaningless by itself.
for(int i=1; i<n; i = i + inc){
System.out.println(i);
}
CrispyChicken
10-16-2014, 06:05 PM
Google: Java For Loop
First link
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics) (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html)
Example from that page.
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
Edit for your needs.
Answer in spoiler but not much of a point in just copying it. Learning to find and use examples (steps above) is the most important lesson. Step below (answer) is meaningless by itself.
for(int i=1; i<n; i = i + inc){
System.out.println(i);
}
Doh! I had the right answer, I just set n and inc to 0 in Java when they were supposed to be fixed values. Setting them to actual values makes it work xD
Thanks though, for some reason reading your post helped me realize my mistake.
Seahawk
10-16-2014, 07:09 PM
Thanks though, for some reason reading your post helped me realize my mistake.
Having a buddy look over your stuff is very beneficial in coding.....but only one person at a time, not more. Three or more way coding never works.
Kong is right, the important part is you learn what happened here, this is why I put code in spoilers.
Just curious, do you plan to continue programming classes after this class?
Krendall
10-16-2014, 07:12 PM
Ahh, Java. That takes me back. A shame I don't really remember any of it. :-[
CrispyChicken
10-16-2014, 07:14 PM
Having a buddy look over your stuff is very beneficial in coding.....but only one person at a time, not more. Three or more way coding never works.
Three way coding?
http://www.retrouprising.com/img/2014_10/4761.gif
Just curious, do you plan to continue programming classes after this class?
Yep, computer science is my major. Not sure specifically what I want to do but pretty sure it'll be programming related.
Kugyince
10-16-2014, 08:08 PM
Nearly all of the coding jobs have been outsourced to India. You'll need to really creative, lucky, or find another line of work.
Slime Slayer
10-16-2014, 09:51 PM
Nearly all of the coding jobs have been outsourced to India. You'll need to really creative, lucky, or find another line of work.
Or be damn good.
bearhawk72
10-17-2014, 08:37 AM
Or be damn good.
According to my CS friends, no you really don't. As in everything else, it's more who you know not what you know. The stories they tell of people who can't code anything at all but still have coding jobs are insane.
They also say good coding jobs are hard to find. You'll either work for a massive company like Google who will put you under ridiculous deadlines and demands(which is why they offer so much at their HQ, they don't want you to leave the office) or you'll be a contractor where you are employed by one company who will farm you out to other companies on a contact basis.
Kugyince
10-17-2014, 12:23 PM
Or be damn good.
I think that falls under creative if it's even a factor at all.
Slime Slayer
10-17-2014, 03:35 PM
I figured creative meant in how you land the job. I really don't know jack about it, but I just kind of assumed it would be like anything else, the cream rises to the top...
Seahawk
10-17-2014, 03:49 PM
Guys all he wanted was homework help.
Kugyince
10-17-2014, 06:23 PM
Conversation naturally flows and evolves. His questions were answered so why not lead the thread into something interesting? Better than just another dead thread.
CrispyChicken
10-17-2014, 06:37 PM
Well this won't be the last time I'll be using this thread. I'm sure I'll be stuck on a lot of problems in the future.
Slime Slayer
10-17-2014, 06:52 PM
Exactly, and we're already covering the inevitable I can't find a job problem preemptivly! Remember, it's all about efficiency when dealing with code.
CrispyChicken
10-17-2014, 06:59 PM
It's ok, I got that Pro Tying Speed ;)
Kugyince
10-17-2014, 07:14 PM
It's ok, I got that Pro Tying Speed ;)
Great, that will help you fill out unemployment and welfare documents that much faster.
I figured creative meant in how you land the job.
My first real job was at Microsoft. I was 20, a high school dropout and the only computer I'd ever used was an old Atari ST. I failed the interview but then just followed all of the new hires (the guys that passed their interview) and pretended like I was with them. No one ever noticed I wasn't supposed to be. First thing I did was ask the guy next to me how to send an email. I set a record in my department for most promotions in first year. When I quit I was in charge of 200 people. During one interview for a promotion HR asked what I did to prepare for the interview. I said I bought a new suit and a haircut. She said I got the promotion mostly because of that answer; it showed I didn't need to cram for the technical part of the interview. Reality, the interviewer just didn't know enough to ask any technical questions.
I think that proves that HR must have been very creative when getting their jobs. They weren't qualified to interview anyone. It was dotcom bubble though. No one was qualified for anything they were doing back then.
While I was working for MS I applied at ID. They turned down in the first 2 minutes of the interview because I wore a tie and short sleeve shirt. They said MS boys were too stuffy and ID was a tshirt and jean company. MS was super casual too, but still you dress up for an interview. ~1999.
Slime Slayer
10-17-2014, 10:37 PM
My first real job was at Microsoft. I was 20, a high school dropout and the only computer I'd ever used was an old Atari ST. I failed the interview but then just followed all of the new hires (the guys that passed their interview) and pretended like I was with them. No one ever noticed I wasn't supposed to be. First thing I did was ask the guy next to me how to send an email. I set a record in my department for most promotions in first year. When I quit I was in charge of 200 people. During one interview for a promotion HR asked what I did to prepare for the interview. I said I bought a new suit and a haircut. She said I got the promotion mostly because of that answer; it showed I didn't need to cram for the technical part of the interview. Reality, the interviewer just didn't know enough to ask any technical questions.
I think that proves that HR must have been very creative when getting their jobs. They weren't qualified to interview anyone. It was dotcom bubble though. No one was qualified for anything they were doing back then.
While I was working for MS I applied at ID. They turned down in the first 2 minutes of the interview because I wore a tie and short sleeve shirt. They said MS boys were too stuffy and ID was a tshirt and jean company. MS was super casual too, but still you dress up for an interview. ~1999.
That's friggin awesome, reminds me of the time my entire department was getting canned. They asked all of us to go down to a room for an emergency meeting. I had suspected that something was up so I didn't go. When the IT guy came around to log everyone out of their computers for the last time,(to prevent mass deletion in a fit of rage) he saw me still sitting there, I told him I was told not too attend the meeting. I was moved to a new department, and promptly got a promotion, -witch I can't explain. Not quite as epic as your story Kong, but in the same vein so I figured I'd share.
CrispyChicken
10-19-2014, 06:15 PM
http://www.retrouprising.com/img/2014_10/4791.jpg
Any reason the ? operator isn't being identified by the system? Or am I doing something wrong?
Slime Slayer
10-19-2014, 06:38 PM
I really wanna take some guesses but I'd make a fool of myself lol. Good luck.
Seahawk
10-19-2014, 06:51 PM
The form for ternary operator is
variable = (condition test) ? resultTrue : resultFalseIf the condition test is true, variable will equal resultTrue.
Otherwise, variable will equal resultFalse.
Your code is
condStr = (userVal < 0) ? 0 : userValcondStr is a string.
Your results for the operation are 0 and the current value of userVal. These are integers.
Error: You are trying to write an integer value to a string.
CrispyChicken
10-20-2014, 04:12 PM
Thank you. Didn't know the expression changes when it's a string. Got it to work.
---------- Post added at 11:16 PM ---------- Previous post was at 08:28 PM ----------
1. http://www.retrouprising.com/img/2014_10/4793.jpg
2. http://www.retrouprising.com/img/2014_10/4794.jpg
3. http://www.retrouprising.com/img/2014_10/4795.jpg
Last three problems I have for my HW. I'm completely lost on these three...
---------- Post added 10-20-2014 at 05:12 PM ---------- Previous post was 10-19-2014 at 11:16 PM ----------
I managed to solve the first one. I'm working on the second one and I think I'm close. Still have no idea how to do the third though.
bearhawk72
10-20-2014, 04:33 PM
My first real job was at Microsoft. I was 20, a high school dropout and the only computer I'd ever used was an old Atari ST. I failed the interview but then just followed all of the new hires (the guys that passed their interview) and pretended like I was with them. No one ever noticed I wasn't supposed to be. First thing I did was ask the guy next to me how to send an email. I set a record in my department for most promotions in first year. When I quit I was in charge of 200 people. During one interview for a promotion HR asked what I did to prepare for the interview. I said I bought a new suit and a haircut. She said I got the promotion mostly because of that answer; it showed I didn't need to cram for the technical part of the interview. Reality, the interviewer just didn't know enough to ask any technical questions.
I think that proves that HR must have been very creative when getting their jobs. They weren't qualified to interview anyone. It was dotcom bubble though. No one was qualified for anything they were doing back then.
While I was working for MS I applied at ID. They turned down in the first 2 minutes of the interview because I wore a tie and short sleeve shirt. They said MS boys were too stuffy and ID was a tshirt and jean company. MS was super casual too, but still you dress up for an interview. ~1999.
It seems to me like the ID people had already hired someone and were just looking for a way to get you gone quickly.
CrispyChicken
10-20-2014, 04:40 PM
I managed to solve the first one. I'm working on the second one and I think I'm close. Still have no idea how to do the third though.
-_- Nevermind, can't get either of these.
It seems to me like the ID people had already hired someone and were just looking for a way to get you gone quickly.
Nah. Was just a 20 year old doing the interview and had stupid business sense of a 20 year old.
CrispyChicken
05-06-2015, 06:47 PM
OK, I'm doing an extra credit project for my Comp Sci class. I need to implement a Bridge Card game. Basically what I'm having trouble with is distributing 13 cards to the 4 players. I made the deck of 52 cards but don't know how to give each "hand" 13 cards. Any tips?
Seahawk
05-06-2015, 08:41 PM
1. Implement your cards. At minimum you only need two variables, that will represent the number value and the suit. Maybe you'll want or need a third variable to represent color (or you can always use the suit as the color since heart/diamond are always red and spade/club are always black).
2. Implement the player hands. You can represent this with arrays of 13 entries, or another data structure (a stack will work fine too).
3. Use a List (java.util.List) (http://docs.oracle.com/javase/8/docs/api/java/util/List.html) object to represent your deck, and insert your cards into it. You can insert them in any order. Make an integer variable and set it to 52, to represent that there are 52 cards in the deck to choose from.
4. Use the Random class (java.util.Random) (http://docs.oracle.com/javase/8/docs/api/java/util/Random.html) to generate random numbers that remove a card from the list and give it to one of the four player arrays in sequence. Loop this until the list is empty. Here's a sample
List<card> deck;
int cards_left = 52;
while(cards_left > 0)
{
player1.givecard( deck.remove( nextInt( cards_left ) ) );
cards_left--;
player2.givecard( deck.remove( nextInt( cards_left ) ) );
cards_left--;
player3.givecard( deck.remove( nextInt( cards_left ) ) );
cards_left--;
player4.givecard( deck.remove( nextInt( cards_left ) ) );
cards_left--;
}
The List automatically shifts items over as they are removed, so you don't have to worry about deck management.
Decrementing cards_left makes sure you don't go outside the bounds of the list.
CrispyChicken
05-06-2015, 10:08 PM
Meh, having trouble across the board. Probably just going to give up on this one. Just getting frustrated now which will only make things worse.
Thanks for the response though SH.
CrispyChicken
09-08-2015, 09:14 PM
Got a question. I'm making a menu with several inputs and once the user inputs one, it calls a specific method from a different class.
For example, if the user inputs 'A', the Add Course method I created should run. But I'm having trouble connecting the two. How do I call the Add Course method to run once the user enters 'A'? I feel like I've done this before, yet I can't for the life of me figure this out right now.
vBulletin® v3.8.7, Copyright ©2000-2026, Jelsoft Enterprises Ltd.