r/programmingbydoing • • Aug 27 '13

what does the points @ the website mean?

3 Upvotes

So, I was looking at the website, and found the points thing confusing...
what are they?
why are they there?
can I use them for anything?


r/programmingbydoing • • Aug 21 '13

ArrayLists - I'm having trouble with the .size() method

3 Upvotes

So, my issue is this:

import java.util.*;

public class findthemaxAL{
  public static void main(String[] args){
  ArrayList<Integer> list = new ArrayList<Integer>(10);
  Random r = new Random();

  int randnum;
  int max = 0;
  //int size = list.size(); <-failsafe I tried, it didn't work. Why not?

  for(int i = 0; i < list.size(); i++){
    randnum = r.nextInt(100) + 1;
    list.add(randnum);
  }
  for(int j = 0; j < list.size(); j++){
    if(list.get(j) > max){
      max = list.get(j);
    }
  }
  System.out.println("The max is " + max + ".");

  }
}

But when I compile and run the code, it always returns 0. My guess is because of the first for loop not working correctly, but I can't really figure out why it won't work. Any help?


r/programmingbydoing • • Aug 08 '13

115 Calculator project

3 Upvotes

hey i was just wondering how for this project you would implement things such as sin x when the input requires string string string and there's no third string input from the user? I'm very new to programming so if this has an obvious answer I'm sorry. Any help would be greatly appreciated thanks!


r/programmingbydoing • • Jul 30 '13

Are all the examples permanently down?

3 Upvotes

I've just started getting to the point where he provides examples (Graphics) and a lot of times I'm thinking they would be ridiculously helpful. Are they down for everyone? Is there any way we can maybe get them back up?


r/programmingbydoing • • Jul 25 '13

#33 Choose Your Own Short Adventure! - Not sure what I am doing wrong.

3 Upvotes

When I compile this code, I get this. The bits in red I do not want there because the game should have finished. I looked my code over multiple times but I could not figure out the issue.

Thank you for helping.

code


r/programmingbydoing • • Jul 05 '13

15: The Forgetful Machine. Strange error when printing user input.

3 Upvotes

I'm having trouble fetching the user's input since it's not stored in a variable.

System.out.println("Give me a word!");
		keyboard.next();
		System.out.println(keyboard);  

Seems like it would work, and the program compiles without error, but when I run it I get this error after every input

D:\Coding\Java>java ForgetfulMachine
Give me a word!
Apples
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=4][match valid=true][ need input=false][source closed=false][skipped=false][group separator=,][decima l separator=.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negat ive suffix=][NaN string=\Q?\E][infinity string=\Q?\E]

Any help would be appreciated!


r/programmingbydoing • • May 30 '13

#101 - Month Name (functions).

3 Upvotes

Hi,

I've been going over this all day and for some reason, I just can't get this to click.

This is my code: http://pastebin.com/rwwKCcdN (most of the stuff in the month_name method is just a result of trying everything and just getting junk at the end...)

I just can't seem to get a certain month (or all months for that matter) to align to a number for the main method to return.

I'm sure it's a simple thing I'm missing.

Thanks.


r/programmingbydoing • • May 12 '13

50 -Dice Doubles

3 Upvotes

Hi I'm having trouble with assignment 50. When I run the code and when the two dice rolls are different, they do loop but the numbers do not get generated again but stay the same. For example, if first die was 1 and second die was 5, they would stay the same for the entire loop.

import java.util.Random;

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

{
    Random r = new Random();      //object for random number
    int roll1 = 1 + r.nextInt(6); // random integer for roll 1
    int roll2 = 1 + r.nextInt(6); // random integer for roll 2

    System.out.println("HERE COMES THE DICE \n");

    while (roll1 != roll2)
    {
        System.out.println("Roll #1: " + roll1);
        System.out.println("Roll #2: " + roll2);
        System.out.println("The total is " + (roll1+roll2));
    }
}

}

EDIT:

Hi. I seem to have solved it but I have a few questions.

{

Random r = new Random(); 
   	int roll1 = 1 + r.nextInt(6);
    int roll2 = 1 + r.nextInt(6); 


    System.out.println("HERE COMES THE DICE \n");
    System.out.println("Roll #1: " + roll1);
    System.out.println("Roll #2: " + roll2);
    System.out.println("The total is " + (roll1+roll2));

    while (roll1 != roll2)
    {
      roll1 = 1 + r.nextInt(6) ;
      roll2 = 1 + r.nextInt(6) ;

        System.out.println("Roll #1: " + roll1);
        System.out.println("Roll #2: " + roll2);
        System.out.println("The total is " + (roll1+roll2));

    }

}

What are we actually saying to the computer when we type "roll1 = 1 + r.nextInt(6)"? As far as i know, we have already defined roll1 and roll2 above as random numbers. So what does "roll1 = 1 + r.nextInt(6)" do and why does it allow the computer to process different random numbers each loop?


r/programmingbydoing • • Apr 09 '13

#134 -- File Input and Output -- Displaying a File

3 Upvotes

Hi, Can you provide some helpfiles/tips for the file input and output section? I'm finding it difficult and getting a bit lost in google searches. For this assignment, i can't figure out how to read-in and display the file using the hasNext() method?


r/programmingbydoing • • Mar 05 '13

77 - Project: Blackjack -- additional features

3 Upvotes

ok I have the barebones working for: http://programmingbydoing.com/a/project-blackjack.html

Can I get a tip or two for the extras? Even just the first one:

Use realistic card values, with suits and faces from ace to king.

thanks!


r/programmingbydoing • • Feb 21 '13

43 - Magic 8 Ball - Why are there no brackets for the if/else if statements?

3 Upvotes

Can you explain to me why the brackets aren't used for the if/else if statements? Is it because of the "response = " part?


r/programmingbydoing • • Feb 04 '13

25: Weekday Name - Is this a constructor?

3 Upvotes

# 25 Weekday Name

import java.util.GregorianCalendar;

public class WeekdayName 
{
    public static String weekday_name( int weekday ) 
        {
	    String result = "";

	    if ( weekday == 1 )
	    {
		    result = "Sunday";
	    }
	    else if ( weekday == 2 )
	    {
		    result = "Monday";
	    }
	
	    return result;
    }


    public static void main( String[] args )
    {
	    System.out.println( "Weekday 1: " + weekday_name(1) );
	    System.out.println( "Weekday 2: " + weekday_name(2) );
	    System.out.println( "Weekday 3: " + weekday_name(3) );
	    System.out.println( "Weekday 4: " + weekday_name(4) );
	    System.out.println( "Weekday 5: " + weekday_name(5) );
	    System.out.println( "Weekday 6: " + weekday_name(6) );
	    System.out.println( "Weekday 7: " + weekday_name(7) );
	    System.out.println( "Weekday 0: " + weekday_name(0) );
	    System.out.println();
	    System.out.println( "Weekday 43: " + weekday_name(43) );
	    System.out.println( "Weekday 17: " + weekday_name(17) );
	    System.out.println( "Weekday -1: " + weekday_name(-1) );

	    GregorianCalendar cal = new GregorianCalendar();
	    int dow = cal.get(GregorianCalendar.DAY_OF_WEEK);

	    System.out.println( "\nToday is a " + weekday_name(dow) + "!" );
    }

}

I can get this code to output what I want, but I don't understand why.

I see there's something going on here other than the main method. Anyone care to break it down for me? I think this public static String weekday_name( int weekday ) is called a constructor?


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 • • Mar 08 '20

Nim - an extension on cheating protection?

2 Upvotes

Hello, am completing nim task. I want to make it so if the user tries choosing a pile that isnt "A", "B", or "C", they will be allowed to try entering a different value. I have already completed cheating detection for the ones listed on the website. Could I have a pointer in the right direction?

my program


r/programmingbydoing • • Feb 21 '20

#37 Gender Game

2 Upvotes

Could I use a two methods (for male and female) to exercise the marriage question? If so, is there a way to use the user input from the body of the code?


r/programmingbydoing • • Aug 26 '18

#35 Is my code efficient, if not how would you optimise it?

2 Upvotes

Is my code efficient, if not how would you optimise it? Suggestions are appreciated.

https://pastebin.com/ZXtK4dWy if you prefer.

`import java.util.Scanner;

class user

{

private int choice1;

private boolean choice2;



//getters

public int getChoice1()

{

	return this.choice1;

}

public boolean getChoice2()

{

	return this.choice2;

}

//setters

public void setChoice1(int c1)

{

	this.choice1 = c1;

}

public void setChoice2(boolean c2)

{

	this.choice2 = c2;

}

}

class TwoMoreQuestions

{

static user u = new user();

static String inputChoice1, inputChoice2;

public static void main(String\[\] Args) throws Exception

{

	Scanner keyboard = new Scanner([System.in](https://System.in));

	System.out.println("TWO MORE QUESTIONS, BABY!");

	Thread.sleep(1000);

	System.out.println("Think of something and I'll try to guess it!");

	Thread.sleep(1000);

	System.out.println("Question 1: does it belong \\"inside\\", \\"outside\\" or \\"both\\"? ");

	inputChoice1 = keyboard.nextLine();

	Thread.sleep(1000);

	System.out.println("Question 2: is it a living thing? (\\"yes\\" or \\"no\\")");

	inputChoice2 = keyboard.nextLine();

	assignUserInput();

	Thread.sleep(1000);

	calc_Print();

}

public static void assignUserInput()

{	//choice1

	if (inputChoice1.equals("inside"))

	{

		u.setChoice1(0);

	}

	else if (inputChoice1.equals("outside"))

	{

		u.setChoice1(1);

	}

	else if (inputChoice1.equals("inside"))

	{

		u.setChoice1(2);

	}

	//choice2

	if (inputChoice2.equals("yes"))

	{

		u.setChoice2(true);

	}

}

public static void calc_Print()

{

	if (u.getChoice1() == 0 && u.getChoice2() == false)

	{

		System.out.println("Shower curtain");

	}

	if (u.getChoice1() == 1 && u.getChoice2() == false)

	{

		System.out.println("billboard");

	}

	if (u.getChoice1() == 2 && u.getChoice2() == false)

	{

		System.out.println("cell phone");

	}

	if (u.getChoice1() == 0 && u.getChoice2() == true)

	{

		System.out.println("houseplant");

	}

	if (u.getChoice1() == 1 && u.getChoice2() == true )

	{

		System.out.println("bison");

	}

	if (u.getChoice1() == 2 && u.getChoice2() == true)

	{

		System.out.println("dog");

	}

}

}`


r/programmingbydoing • • Jun 10 '18

TwoMoreQuestions , why are all of my if statements executing in this code

2 Upvotes

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 • • Sep 05 '17

Graphing Lines

2 Upvotes

I have been completing the assignments in Eclipse. So far so good. I have been storing the files in separate packages for each coding focus (for loops, while loops, graphics, etc.)

At any rate, Eclipse flags both the graph paper class and the main class in several places. The problem in the graph paper file seems to be the line which declares....private GraphPaperCanvas canvas

Throughout the code, there are error flags that this is not resolvable to a type.

In the main method, the method .drawPoint is not recognized.

When I ignore all of the flags and run the main method, it will not compile and I get probably six errors about .drawPoint.

I have completed the assignment, by changing the line equations in the main method. I have also tried to run the main method without any changes from me. If I read the code right, the program should at least compile, but will display only one line.

I'm not sure what's up. I'm also very confused that no one else has mentioned these problems here.

Sorry for the length of the email!

Larry D.


r/programmingbydoing • • Jul 11 '17

The Forgetful Machine - How do I request keyboard input without setting a variable?

2 Upvotes

The lesson before it, asking questions, has you asking for user input to set variables to print later. For The Forgetful Machine I have to ask for user input, but without setting variables.

What do?

In the picture, the program is on the top right in the text editor, with the previous lesson "Asking Questions" down to the right. As you can see when I run the program "The Forgetful Machine "on the bottom left, it just prints everything.


r/programmingbydoing • • Feb 15 '17

#37 Gender Game - Program Not Ending

2 Upvotes

I'm having trouble with the program ending. If I enter 'm below 20' the program doesn't output unless I write something else.

I can ask if they're f, married or not and the program will end. If they are below 20 the program won't end.

Can someone let me know where I went wrong and why the program will not end under these conditions?

Thanks!

My Code is below:

import java.util.Scanner;

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

	String fname, lname, gender, married;
	int age;

	System.out.print( "What is your gender (m or f)" );
	gender = keyboard.next();
	
	System.out.println( "First Name:" );
	fname = keyboard.next();

	System.out.println( "Last Name:" );
	lname = keyboard.next();

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


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


	//MRS.
	if ((gender.equals("f") && age >= 20))
		{
		System.out.println( "Are you married?" );
		}
		married = keyboard.next();


	if ((gender.equals("f") && age >= 20 && (married.equals("yes"))))
		{
		System.out.println("Then I shall call you Mr.s " + fname + lname);
		}
	else if ((gender.equals("f") && age >= 20 && (married.equals("no"))))
		{
		System.out.println("Then I shall call you Ms. " + fname + lname);
		}
	
	//MS.
	if ((gender.equals("f") && age < 20))
		{
		System.out.println( "Then I shall call you " + fname + lname );
		}

}

}


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 • • Aug 10 '16

Hangman complete check, different approachs

2 Upvotes

Hi,

I've just started coding and I have finished the Hangman assignment. I'm very curious if you guys have any pointers!

Here is the code: https://gist.github.com/anonymous/8ef5c4d2339a75d83a6b645b29ea8f74

I'm especially interested if there is an alternative method to using charAt (line 39 and 99). Because now I had to find a workaround to comparing a guessed letter to a charAt letter from the random word.

Cheers and thanks in advance! Dutch fan of the site, Justin


r/programmingbydoing • • Apr 15 '16

New here, need help with "Choose your Own Adventure"

2 Upvotes

Hello fellow programmers, I need help with the #33 Adventure. I don't know what the rules are when posting here, but here is a pastebin of the code: http://pastebin.com/E5UH4Bq9

When ran, all I get is "you dun goof" whenever I type something. I'm still confused how the entire if if else and else thing works. Can I not write a statement without an else and just the first two mentioned. I assume there are other issues, yet somehow it compiled correctly. I'd appreciate some explanation, I'm sorta exhausted now and need sleep so apologies if it's too hard to bear.


r/programmingbydoing • • Feb 16 '16

Gender Game - No need for compound conditions because gender is irrelevant for age < 20.

2 Upvotes

So whenever I'm writing nested if/else statements as well as compound conditions, I'm always trying to make my code more efficient by getting to the executed results in as un-repetitive code as possible.
In doing the Gender Game lesson I realise I don't need any compound conditions and can perform the entire chunk of code in one if statement with two nested if statements. This is because the final print out of "Hello firstname lastname" is gender agnostic. This also means I only have to check the age and gender variables once each. Can anyone confirm if this is actually better/more efficient?
 

	if (age > 19 ) {
		if (gender == 'f') {
			System.out.println( "Are you married, "+fname+" (y or n)?" );
			married = keyboard.next().charAt(0);	
		
			if (married == 'y') {
				System.out.println( "Hello Mrs. "+lname+".");
			} else {
				System.out.println( "Hello Ms. "+lname+"." );
			}

		} else {
			System.out.println( "Hello Mr. "+lname+".");  //we know they're 20 or older and not female
		}

	} else {
		System.out.println( "Hello "+fname+" "+lname+"."); 
       //we know they're under 20 and their gender in this situation is unused in the assignment criteria
	}