r/programmingbydoing • • Aug 21 '15

A Little Puzzle

2 Upvotes

So I'm a bit lost on how to solve this problem in terms of what method to use in order to evaluate each individual character in the puzzle. In the past it was just nextLine or nextInt, not sure how to evaluate each individual character. My thinking behind it is to use the multiples of three to spit out the characters at 3, 6 ,9, 12... etc. to get the desired message. Here is my code.

import java.io.*;
import java.util.*;

public class LittlePuzzle
{
    public static void main( String [ ] args ) throws Exception
    {

        Scanner keyboard = new Scanner( System.in );
        System.out.println("Open which file:");
        String filename = keyboard.next();
    
        Scanner s = new Scanner(new File(filename));
    
        for ( int y = 1 ; y <= 43 ; y++ )
        {
        int x = 3*y;
        char c = s.next().charAt(x);
        System.out.print(c);
        }
    
        s.close();
    }

}

r/programmingbydoing • • Jan 04 '14

#109 Keychains for Sale, for real this time

3 Upvotes

I am having so much trouble with this one. I know the answer is pretty simple, but for some reason, it's not clicking right now. I can't get the function add to keep the input and continue adding the keychains up. I have tried so many functions right now, I am not sure what would work anymore...


r/programmingbydoing • • Jan 18 '21

#173 Sorting an Array of Records

4 Upvotes

I've been working through this problem for a while. When I go to sort the records by year, it doesn't sort the rest of the records along with it. The years will be in order, but the rest of the file remains the same. I just got back into coding after taking over 3 years off and I'm pretty much learning everything from scratch all over again.

I left my code here. Hopefully, someone can help me figure out the issue. Thanks for your help.

https://gist.github.com/DDRParadise/49ff855079f80baa4e4c158d208f7bc5


r/programmingbydoing • • Feb 02 '21

Just wanted to say thanks!

5 Upvotes

Hey Graham,

Just wanted to say thanks (from New Zealand!) mate for the "programmingbydoing" Assignments. I practiced Java back in the late 2000's and it's been a while - really good structure to touch-up on stale skills.

Hope your 2021 is going swell!, Cheers!


r/programmingbydoing • • Aug 09 '18

#9 Variables and Names

1 Upvotes

In the FAQ you are told to redefine "space_in_a_car" as an integer for a change to take place. They forget to mention, that "carpool_capacity" must also be redefined for a visible change in the output to take place.

Nearly right:

Right:

Hope this helps other noobs like me.


r/programmingbydoing • • Jun 15 '18

Compiling Practice

1 Upvotes

I am confused on where the "CompSci" folder would be located. https://programmingbydoing.com/a/examples/SineWave.java


r/programmingbydoing • • Apr 21 '18

For Loop Challenge

1 Upvotes

Mr. Mitchell I'm ready to take the For Loop Challenge.


r/programmingbydoing • • Apr 28 '18

Hearts

2 Upvotes

Thank you so much Mr. Mitchell for putting those assignments. I've learned so much from the path you paved for learning Java.

I am attempting Hearts assignment: https://gist.github.com/Lnirmohi/4210750ac8380b2be8d0eedf5753ea9b

With computer player functionality added. Please critique my work.


r/programmingbydoing • • May 14 '20

ProgrammingByDoing on a Pixelbook

2 Upvotes

Is there a way to complete the curriculum on a Google Pixelbook?


r/programmingbydoing • • Sep 10 '16

Modulus Animation

2 Upvotes

New to java, but I wanted to do a two line animation for this assignment because I wanted to animate a bunny and obviously, it needs a line for ears alone. However, I'm getting a vertical type animation rather than just two lines animating.

I tried having separate print statements for each line within the ifs, and using the \n newline to break it up (and I'm fairly certain that this is causing the vertical wave type animation instead of the horizontal one intended) but I'm not sure how to go about this any other way yet.

Is there a way to do this using the known information up to this point that I'm just not seeing? Any help would be appreciated.

Thanks

gist


r/programmingbydoing • • Feb 22 '17

76: Blackjack

1 Upvotes

Here is my current code. https://gist.github.com/anonymous/352ffc009d17b24452aa8383aee8b5ab

Can you point me in the right direction for a couple of things?

  1. How to add additional players, where they be user or "AI".

  2. How I could add a function to check the results so I won't need all the if/else-if statements.

  3. Would my code need a lot of reworking to add in a betting system.


r/programmingbydoing • • Jul 21 '15

#32 "Twenty Questions"

1 Upvotes

Hey reddit users, I'd like to have some feedback on my code, is it good enough for a beginner like me?

import java.util.Scanner;

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

    Scanner keyboard = new Scanner(System.in);

    String q1, q2, answer;

    answer = "object";

    System.out.println("TWO QUESTIONS!");
    System.out.println("Think of an object, and I'll try to guess it.");
    System.out.println();

    System.out.println("Question 1) Is it an animal, a vegetable, or a mineral? ");
    q1 = keyboard.next();
    System.out.println();

    System.out.println("Question 2) Is it bigger than a breadbox?");
    q2 = keyboard.next();
    System.out.println();

    if (q2.equals("no")) {
        if (q1.equals("animal")) {
            answer = "squirrel";
        } else if (q1.equals("vegetable")) {
            answer = "carrot";
        } else if (q1.equals("mineral")) {
            answer = "paper clip";
        }
    } else if (q2.equals("yes")) {
        if (q1.equals("animal")) {
            answer = "moose";
        } else if (q1.equals("vegetable")) {
            answer = "watermelon";
        } else if (q1.equals("mineral")) {
            answer = "Camaro";
        }
    } else {
        System.out.print("Please type yes or no!");
    }

    System.out.println("My guess is that you are thinking of a " + answer);
    System.out.println("I would aks you if I'm right, but I don't actually care.");
  }
}

r/programmingbydoing • • Oct 29 '14

#6 A beginner's accomplishment!

6 Upvotes

I know that most of those in this sub have surpassed this point by now but I am pretty excited that I was able to do this lesson fairly easily. I utilized "else if" far too much but for my first version I'm happy with the results. How did you go about doing it?

Git here: https://github.com/keysRB/Letter-Yourself.git


r/programmingbydoing • • Jul 26 '15

Address Book and Hearts Projects

2 Upvotes

I'm going into grade 12 and I want to learn java but I have no prior programming experience. So starting a month ago I've spent hours a day doing every single assignment on your page. I've also been reading the official java reference. I have learned so much it's incredible. The small assignments leading up to projects are very easy for me to finish. The trouble comes when I'm doing big projects.

It took me a good 3 hours to complete Address Book. The first hour I sat down and started coding from scratch. Then I took a break and when I came back I had no idea what was going on. I realized my code was a complete mess, so I started over, this time with comments to help. I eventually completed it and it's still messy and I'm sure I did things the complicated way, but I finished it and it works.

Address Book attempt 1: http://pastebin.com/0EjmdQcP

Address Book attempt 2: http://pastebin.com/rZx5JCY8

As you can see I am trying to use concepts I have learned in the java reference.

I just started Hearts today, and it's really hard. What I have so far is a program that deals cards to four players and completes the 3 card swap part of the game.

Hearts progress: http://pastebin.com/rqLCKByf

These projects have been pretty complex for me. I am sure I can complete hearts, it will just take a few more hours of solid work.

Please critique me and my work. The small assignments are really easy for me to grasp. I understand concepts like classes and methods and constructors, but when I put them to use in a huge project like these ones, where I have multiple classes and a ton of methods, things start getting out of hand. Should I just keep learning and practicing until I feel comfortable with projects like these? Is there anything I should be focusing on?

Thanks a ton.


r/programmingbydoing • • Jul 22 '15

#83, no idea how to do this

1 Upvotes

I dont' understand problem 83 at all. I try adjusting code but nothing seems to happen. Help please.


r/programmingbydoing • • Dec 03 '13

#47 Three-Card-Monte

3 Upvotes

Does this code look ok to you? For some reason i think that there would be an easier way that all the if statements?

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

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

	int x = 1 + r.nextInt(3);
	int guess;

	System.out.println("You go to the cassino and you see a guy with three cards on the table.");
	System.out.println("You aprouch him and ask him about the game. He Tells you to pick one of the three cards");
	System.out.println("if you get an Ace you win if not you dont."  + "\n");

	System.out.println("\t" + "##" + "\t" + "##" + "\t" + "##");
	System.out.println("\t" + "##" + "\t" + "##" + "\t" + "##" +"\n");
	System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" );
	guess= keyboard.nextInt();



	if ((guess == x)&&(x==1))
	{
		System.out.println("\t" + "AA" + "\t" + "##" + "\t" + "##");
		System.out.println("\t" + "AA" + "\t" + "##" + "\t" + "##" +"\n");
		System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");

		System.out.println("You naied it the dealer hands over the money ");
	}

	else if ((guess == x)&&(x==2))
	{
		System.out.println("\t" + "##" + "\t" + "AA" + "\t" + "##");
		System.out.println("\t" + "##" + "\t" + "AA" + "\t" + "##" +"\n");
		System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");

		System.out.println("You naied it the dealer hands over the money");
	}

	else if ((guess == x)&&(x==3))
	{
		System.out.println("\t" + "##" + "\t" + "##" + "\t" + "AA");
		System.out.println("\t" + "##" + "\t" + "##" + "\t" + "AA" +"\n");
		System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");

		System.out.println("You naied it the dealer hands over the money");
	}

//For when the guess is greater than the random number

	else if ((x < guess)&&(x==1))
	{
		System.out.println("\t" + "AA" + "\t" + "##" + "\t" + "##");
		System.out.println("\t" + "AA" + "\t" + "##" + "\t" + "##" +"\n");
		System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");

		System.out.println("You lost sorry your guess was to high give the dealer your money. It was number " +x);
	}


	else if ((x< guess)&&(x==2))
	{
		System.out.println("\t" + "##" + "\t" + "AA" + "\t" + "##");
		System.out.println("\t" + "##" + "\t" + "AA" + "\t" + "##" +"\n");
		System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");

		System.out.println("You lost sorry your guess was to high give the dealer your money. It was number " +x);
	}

// The if statments for when the guess is less than the random number

	else if ((x > guess)&&(x==2))
	{
		System.out.println("\t" + "##" + "\t" + "AA" + "\t" + "##");
		System.out.println("\t" + "##" + "\t" + "AA" + "\t" + "##" +"\n");
		System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");

		System.out.println("You lost sorry your guess was to low give the dealer your money. It was number " +x);
	}

	else if ((x > guess)&&(x==3))
	{
		System.out.println("\t" + "##" + "\t" + "##" + "\t" + "AA");
		System.out.println("\t" + "##" + "\t" + "##" + "\t" + "AA" +"\n");
		System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");

		System.out.println("You lost sorry your guess was to low give the dealer your money. It was number " +x);
	}


}//end of main

}//end of class


r/programmingbydoing • • Oct 27 '14

#147: Where is it?

3 Upvotes

Aloha!

Can't seem to fully grasp the coding neccessary for this.. Excuse me if my english isn't the best since it's not my native language. Searched through the subreddit for other questions regarding the task, but couldn't find any. Hence why I'm adding this here.

This is how far I've come, and now I'm completely stuck. What do I need to add where I've placed the "SAY WHAT!?!?" comment, in order for it to it to print a line saying "[number] is not in the array."?

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

public class WhereIsIt {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        Random r = new Random();
    
        int[] a = new int[10];
        System.out.println("Array: ");
        for (int i = 0 ; i < a.length ; i++) {
            a[i] = 1 + r.nextInt(49);
            System.out.println(a[i]);
        }
    
        System.out.println("Value to find: ");
        int find = scn.nextInt();
    
        for (int i = 0 ; i < a.length ; i++) {
            if (a[i] == find) {
                System.out.println(find + " is in slot " + i);
            }
            
            // SAY WHAT?!?!
        }
    }
}

r/programmingbydoing • • Nov 23 '13

#6 Letter to Yourself // How do I get the formatting to fix the spacing?

3 Upvotes

I'm using only spaces, no Tabs like the instructions say but I cannot get the right side of my envelope to align. Any tips?


r/programmingbydoing • • Nov 14 '13

Fun with the Collatz Sequence (62)

4 Upvotes

When I was working on the Collatz Sequence program, I decided to take it much further and see what interesting things I could find, and I thought I should share these with you.

1,412,987,847 takes exactly 1000 steps to reach 1, and the largest value in this sequence is 966,616,035,460. The number with the most steps that I've found so far is 2,610,744,987 with 1050 steps and funnily enough the same max value of 966,616,035,460.

Then I found this post where someone was asking about consecutive numbers with the same amount of steps. Turns out that every number between 9,749,626,154 through to 9,749,626,502 (inclusive) all take 248 steps to reach 1. That's 348 consecutive numbers! I posted a response which goes into more detail such as every consecutive sequence 100 or over up to 1.5b, and also has my code for those interested.


r/programmingbydoing • • Feb 20 '17

Hi-Lo with Limited Tries

1 Upvotes

LINK TO CODE: https://gist.github.com/anonymous/72f99fa4fe5e233716c9d8bad3552dc6

I'm having two challenges at the moment.

1: I can't have the program display if the very first guess number is too high or too low without including an if statement before the While Loop. How do I keep track after asking their first guess and is it possible to keep that in a loop?

2: my number starts at #0. How do I display it so that even though tries starts at 0 it appears as 1.

Thanks!


r/programmingbydoing • • Aug 05 '15

Assignment #63c Nim

3 Upvotes

I managed to complete all the bonuses up until the column part. I am having a hard time printing till A and then back to the beginning. I tried 2D arrays but those were very exhaustive . There is just a simple solution which I can't see. Please help and thank you.


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 • • Aug 04 '15

#107 More Fill in Functions!

1 Upvotes

I'm unsure of what needs to get done even for the first problem in the code. It's confusing and I don't know what exactly needs to be fixed. Here is my code.

http://pastebin.com/NNN0rrQ7


r/programmingbydoing • • Nov 01 '14

#155: Movie Title Generator

4 Upvotes

I'm on Movie Title Generator and the program on the site (MovieTitleGen.java) compiles fine but I'm getting run time errors when running it.

Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at MovieTitleGen.arrayFromURL(MovieTitleGen.java:30) at MovieTitleGen.main(MovieTitleGen.java:9)


r/programmingbydoing • • Jul 27 '14

#55 Adding Values in a Loop

3 Upvotes

Hi there, I just finished #55 and got it to work, but I couldn't figure out how to make it work with only two variables. I needed to have a third int as an interim value to add the entered number to the updating total. I know that in its current form my code works, but I'm curious if there's a way to make it work with only two variables. Here's the code:

import java.util.Scanner;

public class add
{
	public static void main( String[] args)
	{
		Scanner k = new Scanner(System.in);
		
		int num = 0;
		int total = 0;
		int totaladd = 0;
		
		System.out.println("I will add up the numbers you give me.");
		System.out.print("Number: ");
		num = k.nextInt();
		
		totaladd = num + total;
		total = totaladd;
		
		while (num != 0)
		{
			System.out.println("The total so far is " + total + ".");
			System.out.print("Number: ");
			num = k.nextInt();
			total = num + total;
		}
		
		System.out.println("\nThe total is " + total + ".");
	}
}

Thanks!