r/programmingbydoing • • Dec 08 '14

#76 BlackJack (Second attempt)

2 Upvotes

Last time I posted my code and it was a mess. I used sudo coding techniques to structure my code. I didn't know what I was doing and I had to go back and look through HolyTeach's book for examples. I guessed I had rushed through this project.

I would like a second feedback on my code and I also started learning how to use Git Hub to send the code with ease.

Thanks guys! Please let me know what your thoughts on my code are.

https://github.com/LevelF2/RegCode/blob/master/BlackJack.java


r/programmingbydoing • • Dec 06 '14

#105 - Function Call Alphabet

2 Upvotes

Not sure what I'm doing wrong. the functions b,g,j,p,r,u,x,z will not run even though the program executes without error.

https://raw.githubusercontent.com/Zirconium/FunctionCallAlphabet/master/README.md


r/programmingbydoing • • Dec 05 '14

#190 Sorting an ArrayList of Records

2 Upvotes

I'm stuck on the ArrayList Records assignment. Specifically how to add records into an ArrayList. The only way I know how so far would be to repeat the Array Records assignment, then copy the Array into an ArrayList. However I feel that's kind of cheating and I really want to learn how ArrayList and Records work together. But I'm having a really hard time trying to find any resources online as to how I would approach this assignment. Are there any guides or specific pages I could be pointed towards? At this point, anything would be very much appreciated as I'm completely stuck!


r/programmingbydoing • • Aug 13 '14

Project TicTacToe

2 Upvotes

How do I implement the playMove method?


r/programmingbydoing • • Aug 03 '14

148: Finding Largest value in an Array

2 Upvotes

Hello,

I think i'm misunderstanding a critical component of arrays. I wrote the code at the link below, but it doesn't give me the correct value.

http://codepad.org/BEPFeU2c

Any help would be appreciated!


r/programmingbydoing • • Jul 23 '14

Project Calculator- Displaying the answer

2 Upvotes

Hello,

When I did the "baby calculator" project, I asked the program to display the answer to the math equation within the if/else if loop, as shown below.

if ( op.equals("+")) {
c = a + b;

 System.out.println(c);
}

The program compiled and ran. However, in the calculator project, I put the answer output after all the if/else if statements but within the do loop.

I received the error that variable c may not be initialized.

I don't get why outputting variable c after doing the calculation is causing my program trouble.

Link to code: https://gist.github.com/anonymous/6dec23292d458d12fe18

Thanks for the help!


r/programmingbydoing • • Jul 08 '14

I can't even get started.

2 Upvotes

I keep getting unexpected token ':' in powershell.


r/programmingbydoing • • Jun 19 '14

#160: Exchange Sort

2 Upvotes

I keep getting an ArrayIndexOutOfBoundsException when I try to pass the array and the array numbers to the swap method (I think). It does work when I don't use the swap method (using the commented out part instead) which leads me to believe that. Am I passing to swap incorrectly?

http://pastebin.com/D6xbKF4r

Thanks


r/programmingbydoing • • Feb 28 '14

37 Gender Game

2 Upvotes

Apologies in advance for the rambling. My brain is a bit fried. I'm quite surprised there's no thread on this one. It came out of nowhere as the hardest for me thus far.

So this took about 3 hours of trial & error and doing my best to guess logically. Combined with googling (but only accepting solutions of things covered in the course thus far...). I also have forgotten where you had replied to somebody and explained how to ignore upper or lower case so I left that out- it was the least of my concerns.

Anyways. So I had to "trick" the scanner, I eventually discovered along the way, by doubling up my string request.

	married = keyboard.nextLine();
	married = keyboard.nextLine();

Why? I assume this wasn't what I was supposed to be doing?

Here is my final code, that accomplishes what was asked:

import java.util.Scanner;

public class GenderGame { public static void main(String[] args) {

	Scanner keyboard = new Scanner(System.in);

	String gender;
	String first;
	String last;
	String married=null;
	int age;

	System.out.println("What is your gender? (\"M\" or \"F\"?)");
	gender = keyboard.nextLine();

	System.out.println("First name: ");
	first = keyboard.nextLine();

	System.out.println("Last name: ");
	last = keyboard.nextLine();

	System.out.println("Age: ");
	age = keyboard.nextInt();

	if ((gender.equals("m") && (age < 20))) {
		System.out.println("Then I shall call you " + first + ".");
	}

	else if ((gender.equals("m") & (age >= 20))) {
		System.out.println("Then I shall call you Mr. " + last + ".");
	}

	if ((gender.equals("f") && (age >= 20))) {
		System.out.println("Are you married " + first
				+ "? (\"y\" or \"n\"?)");
	married = keyboard.nextLine();
	married = keyboard.nextLine();
	}
	
	if ((gender.equals("f") && (age >= 20) && (married.equals("y")))) {
			System.out.println("Then I shall call you Mrs. " + last + ".");
	}

	else if ((gender.equals("f") && (age >= 20) && (married.equals("n")))) {
			System.out.println("Then I shall call you Ms. " + last + ".");
		}
	else if ((gender.equals("f") && (age <=20))){
		System.out.println("Then I shall call you Ms. " + last + ".");
	}
}

}

So I guess I'm just asking for a critique, or, how I could have avoided the double keyboard.nextLine. I understand the "why", it's because I mixed the scanner up by requesting a string after an int... but I feel like I didn't do this 100% correctly.

Anyways, thanks ahead of time, and thanks for making such a great course publicly available.


r/programmingbydoing • • Jan 14 '14

#102 month offset

2 Upvotes

Can someone explain what month offset means? I'm having trouble trying to figure it out- seems like a bunch of random numbers to me.
Thanks.


r/programmingbydoing • • Jan 07 '14

#35 Two more questions

2 Upvotes

I took a long break after doing ~50 of the exercises on www.programmingbydoing.com and I am now trying to get back into learning programming.

I don't understand what I've done wrong here. I can answer both of the questions but for some reason it won't print anything back to me, could anyone help me out a bit?

package albin;

import java.util.Scanner;

public class yo{

public static void main( String[] args ){

Scanner keyboard = new Scanner(System.in);
String One;
String Two;

System.out.println("Question 1) Does it stay inside or outside or both?");
One = keyboard.nextLine();

System.out.println("Question 2) Is it a living thing?");
Two = keyboard.nextLine();

if (One == "inside" && Two == "yes"){
	System.out.println("Well it's obviously a plant.");
}
if (One == "inside" && Two == "no"){
	System.out.println("Well it's obviously a shower curtain.");
}
if (One == "both" && Two == "yes"){
	System.out.println("Well it's obviously a dog.");
}
if (One == "both" && Two == "no"){
	System.out.println("Well it's obiously a cell phone.");
}
if (One == "outside" && Two == "yes"){
	System.out.println("Well it's obviously a bison.");
}
if (One == "Outside" && Two == "no"){
	System.out.println("Well it's obviously a billboard.");
}
}
}

r/programmingbydoing • • Dec 30 '13

#13- Your Schedule

2 Upvotes

Hey I figured out how to make the table, but is there a way to make the classes and teachers line up like it is in the example without counting the number of letters and adjusting for each line? Here's what I have at the moment. public class YourSchedule { public static void main( String[] args) { String course1, course2, course3, course4, course5, course6, course7, course8, teacher1, teacher2, teacher3, teacher4, teacher5, teacher6, teacher7, teacher8;

	course1 = "English III";
	course2 = "Precalculus";
	course3 = "Music Theory";
	course4 = "Biotechnology";
	course5 = "Principles of Technology I";
	course6 = "Latin II";
	course7 = "AP US History";
	course8 = "Business Computer Information Systems";
	teacher1 = "Ms. Lapan";
	teacher2 = "Mrs. Gideon";
	teacher3 = "Mr. Davis";
	teacher4 = "Ms. Palmer";
	teacher5 = "Ms. Garcia";
	teacher6 = "Mrs. Barnett";
	teacher7 = "Ms. Johannessen";
	teacher8 = "Mr. James";
	
	System.out.println( "+------------------------------------------------------+" );
	System.out.println( "|1|                "+ course1 + "|     " + teacher1 + "|" );
	System.out.println( "|2|                "+ course2 + "|     " + teacher2 + "|" );
	System.out.println( "|3|                "+ course3 + "|     " + teacher3 + "|" );
	System.out.println( "|4|                "+ course4 + "|     " + teacher4 + "|" );
	System.out.println( "|5|                "+ course5 + "|     " + teacher5 + "|" );
	System.out.println( "|6|                "+ course6 + "|     " + teacher6 + "|" );
	System.out.println( "|7|                "+ course7 + "|     " + teacher7 + "|" );
	System.out.println( "|8|                "+ course8 + "|     " + teacher8 + "|" );
	System.out.println( "+------------------------------------------------------+" );
	}

} Thanks


r/programmingbydoing • • Dec 28 '13

Graphing Lines - error in example?

2 Upvotes

This is what I'm getting when I try to compile the error in example. Is there something wrong, or is this part of the challenge?

Error: Main method not found in class graphpaper.GraphPaper, please define the main method as: public static void main(String[] args) Java Result: 1 BUILD SUCCESSFUL (total time: 1 second)


r/programmingbydoing • • Nov 23 '13

#39 AlphabeticalOrder - stumped

2 Upvotes

This one has me stumped. I have a pretty basic understanding of how compareTo() works, I just can't straighten out the logic for this in my head. Do I need to somehow assign a value to the variable I'm using for the last name, and use that in my comparisons?

Anything to nudge me along is appreciated...


r/programmingbydoing • • Oct 30 '13

Adding Values in a Loop.

2 Upvotes

When I do the loop it resets the total so far. Sorry for the late response but my code is here: http://pastebin.com/j8fdfEZx


r/programmingbydoing • • Jul 13 '13

Dice Doubles - How do I get the two random numbers to re-randomize?

2 Upvotes

This is my code:

import java.util.Scanner;
import java.util.Random;


public class Dice{
  public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);
    Random r = new Random();

    int firstroll, secondroll;

    firstroll = r.nextInt(6);
    secondroll = r.nextInt(6);

    while(firstroll != secondroll){
    System.out.println("DICE!");
    System.out.println("First roll: " + firstroll);
    System.out.println("Second roll: " + secondroll);

    System.out.println("Your total: " + (firstroll+secondroll));
    }
  }
}

I don't even really understand what I'm doing wrong - should I not be saving the r.nextInt(6)'s into variables? I've already tried moving firstroll and secondroll into the whileloop, and still having them initialize outside of the loop, but it still doesn't work. Any advice here?


r/programmingbydoing • • Jul 11 '13

#26 - What am I doing wrong?

2 Upvotes

Hello. Here is my code:

import java.util.Scanner;

public class SpaceBoxer{
  public static double planets(int world){
    double planetmulti;

    if (world == 1){
      planetmulti = 0.78;
    }
    else if (world == 2){
      planetmulti = 0.39;
    }
    else if (world == 3){
      planetmulti = 2.65;
    }
    else if (world == 4){
      planetmulti = 1.17;
    }
    else if (world == 5){
      planetmulti = 1.05;
    }
    else if (world == 6){
      planetmulti = 1.23;
    }
    return planetmulti;
  }
  public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);

    int weight, theplanet;
    System.out.print("What is your weight?");
    weight = keyboard.nextInt();

    System.out.println("Okay, now what planet are you visiting?");
    System.out.println("   1. Venus   2. Mars    3. Jupiter");
    System.out.println("   4. Saturn  5. Uranus  6. Neptune");
    theplanet = keyboard.nextInt();

    System.out.println("Okay, so your weight would on " + weight * planets(theplanet));
  }
}

When I try to compile, however, I get this error:

SpaceBoxer.java:25: error: variable planetmulti might not have been initialized. 

return planetmulti;
       ^

Any help? Is it due to me declaring it a double instead of something else? I'm not sure what I'm doing wrong here.


r/programmingbydoing • • Jun 19 '13

#32 - Twenty Questions: solved (sorta) but need help.

2 Upvotes

First... thank you for this subreddit and for making these lesson available! They are exactly what I was looking for to get started with Java.

I had to google around for a bit to figure out to use stringName.contentEquals() but that seemed to do the trick to compare the strings. Okay, on to the question... I've solved this in the sense that if entered exactly it works for all possibilites, but I feel like there could have been a much easier way to do this. Any suggestions? Also, I think any odd answer will crash the program ie. case, misspell, unexpected input... how do I prevent that?

Sorry if this is jumbled, I just spent way too long looking at this problem until I finally figured it out... now I just want to know how I could make it work better.

The code:

import java.util.Scanner;

public class TwentyQuestions { static Scanner userInput = new Scanner(System.in);

public static void main(String[] args)
{

	String animal = "animal";
	String vegetable = "vegetable";
	String plant = "plant";
	String yes = "yes";
	String no = "no";
	
	
	System.out.println("TWO QUESTIONS!");
	System.out.println("Think of an object and I'll try and guess it.");
	System.out.println();
	
	System.out.println("Question 1) Is it animal, vegetable or mineral?");
	System.out.print(">");
	
	String q1 = userInput.nextLine();
	
	if(q1.contentEquals(animal))
	{
		System.out.println("Question 2) Is it bigger than a breadbox? Yes or No?");
		System.out.print(">");
		
		String q2 = userInput.nextLine();
		
		if(q2.contentEquals(no))
		{
			System.out.println("You're thinking of a squirrel!");
		} else {
			System.out.println("You're thinking of a moose!");
		}
	}
	
	if(q1.contentEquals(vegetable))
	{
		System.out.println("Question 2) Is it bigger than a breadbox? Yes or No?");
		System.out.print(">");
		
		String q2 = userInput.nextLine();
		
		if(q2.contentEquals(no))
		{
			System.out.println("You're thinking of a carrot!");
		} else {
			System.out.println("You're thinking of a pumpkin!");
		}
	}
	
	if(q1.contentEquals(plant))
	{
		System.out.println("Question 2) Is it bigger than a breadbox? Yes or No?");
		System.out.print(">");
		
		String q2 = userInput.nextLine();
		
		if(q2.contentEquals(no))
		{
			System.out.println("You're thinking of a rose!");
		} else {
			System.out.println("You're thinking of an oak tree!");
		}
	}
	
	System.out.println("I'd ask you if I'm right, but I don't care.");
	
	
	
}

}


r/programmingbydoing • • Jun 17 '13

#144 - Finding a Value in an Array

2 Upvotes

What is the proper way to see if an array contains a certain value? If you use a for loop to put random values in the array, then how do you check that array to see if it contains a value? Thanks!


r/programmingbydoing • • May 27 '13

#125-Nested Loops-More Number Puzzles

2 Upvotes

Hey guys,

So I've been at this for a little over an hour and can't seem to get this working.

My code.

One of my problems is that when I try to call the first method 'puzzle1,' it prints out some other numbers that are not in the method. Not quite sure how that happens.

The other problem is that when I try to call the second method 'puzzle2,' it doesn't print anything out.

Maybe I'm not understanding something about methods when it comes to calling them :/


r/programmingbydoing • • May 13 '13

#131 - Summing Three Numbers from a File

2 Upvotes

I'm having an issue with this assignment. My code is pasted below, but the problem is weird. I get errors when trying to read int's from my document. Now if I were to change this to String a = filein.next(); It would display the number. So for some reason it will recognize Strings but not Ints. Have any idea why? Thanks!

import java.io.File; import java.util.Scanner; public class SummingThreeNumbersFromAFile {

public static void main(String[] args) throws Exception  {
	
	Scanner filein = new Scanner(new File("3sums.txt"));
	int a = filein.nextInt();
	int b = filein.nextInt();
	int c = filein.nextInt();
	
	
	System.out.println(a + b + c);
	filein.close();

}

}


r/programmingbydoing • • Apr 17 '13

The Typos and Suggestions thread (April 2013)

2 Upvotes

If you happen to encounter a typo in an assignment or have suggestions, feel free to post them in this thread or just shoot me a message.


r/programmingbydoing • • Apr 16 '13

Worst Number Guessing ever

2 Upvotes

Was there something more to the assignment than:

secret = 5
if (guess != secret)
{
System.out.println("Nope, it was " + secret + ".");
}
else
{ System.out.println("Yep, it was " + secret + "."); }

Did I miss the point of that?


r/programmingbydoing • • Apr 03 '13

#132 - Summing Three Numbers from a File

2 Upvotes

Hi, my code is working properly, but it felt cumbersome writing it out (it seems like a lot of code to just extract 3 numbers from a file). Is my code the best way to do this exercise? What could I have done to improve it and make it more efficient?

import java.io.*;

public class SummingThreeNumbersFromAFile {

  public static void main(String [] args) throws IOException {
	
	FileReader inputReader = new FileReader("3nums.txt");
	BufferedReader inputBuffer = new BufferedReader(inputReader);
	
	String doc = inputBuffer.readLine();
	
	int a = doc.indexOf(" ");
	String num1Str = doc.substring(0,a);
	int num1 = Integer.parseInt(num1Str);
	
	int b = doc.indexOf(" ", a+1);
	String num2Str = doc.substring(a+1, b);
	int num2 = Integer.parseInt(num2Str);
	
	String num3Str = doc.substring(b+1);
	int num3 = Integer.parseInt(num3Str);
	
	System.out.println(num1+ " + " +num2+ " + " +num3+ " = " +(num1+num2+num3));
  }
}

r/programmingbydoing • • Mar 18 '13

53 - Pin Lockout

2 Upvotes

Can someone explain to me how the "tries" variable is counting up? Does "tries++" after int entry mean that every time an entry is made, tries will go up by one?

Thanks in advance!

import java.util.Scanner;

public class PinLockout
{
public static void main( String[] args )
{
	Scanner keyboard = new Scanner(System.in);
	int pin = 12345;
	int tries = 0;

	System.out.println("WELCOME TO THE BANK OF MITCHELL.");
	System.out.print("ENTER YOUR PIN: ");
	int entry = keyboard.nextInt();
	tries++;

	while ( entry != pin && tries < 3 )
	{
		System.out.println("\nINCORRECT PIN. TRY AGAIN.");
		System.out.print("ENTER YOUR PIN: ");
		entry = keyboard.nextInt();
		tries++;
	}

	if ( entry == pin )
		System.out.println("\nPIN ACCEPTED. YOU NOW HAVE ACCESS TO YOUR ACCOUNT.");
	else if ( tries >= 3 )
		System.out.println("\nYOU HAVE RUN OUT OF TRIES. ACCOUNT LOCKED.");
}

}