MU Soapbox

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Muxify
    • Mustard

    Java Buddies?

    Code
    3
    18
    677
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Cobalt
      Cobalt Tutorialist last edited by

      This isn't MU* code, but not sure where else to put this~

      Do any of you know Java, and are willing to put up with me occasionally sending you messages here (or on skype) about it?

      Glitch 1 Reply Last reply Reply Quote 0
      • Glitch
        Glitch @Cobalt last edited by

        @Cobaltasaurus This should probably go in the coding group forum.

        Cobalt 1 Reply Last reply Reply Quote 0
        • Chime
          Chime last edited by

          I can try to help, but as Glitch says, probably better to take it to that forum.

          I'm really not a Java expert though; haven't used it seriously since 1.1/1.2 era. Public static void main is apparently still a thing though, so I probably have a valid set of basics, even if annotations are new and bizarre looking. Java will probably always be "that odd scripting language that people use for modding cool stuff into Minecraft" to me, but hey, there we go.

          n.b.: you're much better off finding someone who is an enthusiastic user of Eclipse, IntelliJ IDEA, or similar IDEs. I've poked at them but I tend to curmugeonously stick to Emacs.

          It is by will alone I set my mind in motion. It is by the juice of Coffea arabica that thoughts acquire speed, the table acquires stains, the stains become a warning. It is by will alone I set my mind in motion.

          1 Reply Last reply Reply Quote 0
          • Cobalt
            Cobalt Tutorialist @Glitch last edited by

            @Glitch said:

            @Cobaltasaurus This should probably go in the coding group forum.

            I derped. That's what happens when I have to get up at 5:30 in the morning. ^^

            Move it?

            1 Reply Last reply Reply Quote 0
            • Glitch
              Glitch last edited by

              I haven't done any Java since college, but I can also occasionally offer flailing help. Since this is the code category in the code group, though, it's okay to spam your Java queries to the group at large and consensus might save you where individual expertise is lacking. 🙂

              1 Reply Last reply Reply Quote 0
              • Cobalt
                Cobalt Tutorialist last edited by

                Assignment:

                Write a method called lyrics that prints the lyrics of a song when invoked. The method should accept no parameters and return no value.

                public void lyrics()
                {
                	System.out.println.("She got that million dollar.");
                	System.out.println.("Oooh, oooh, oooh.");
                	System.out.println.("She got that million dollar.");
                	System.out.println.("Oooh, oooh, oooh.");
                	System.out.println.("Rich girl, rich girl. I see ya!  Paradise.");
                	System.out.println.("And all I wanna do is touch it, and touch it, and touch it.");
                	System.out.println.("I'm gonna make her tapout, tapout, tapout, tapout.");
                
                }
                
                Chime 1 Reply Last reply Reply Quote 0
                • Chime
                  Chime @Cobalt last edited by

                  @Cobaltasaurus

                  Hmmmm. public makes it accessible outside the class, rather than just inside. Good. void makes it return nothing. Good. () after the name means it takes no parameters. Good.

                  But without static, it's an instance method-- that is, you'd need to invoke it on an object of the class holding that method. If you want it to work without having an instance of that class, it'd be a public static void, much like the traditional main.

                  Also... the . after println seems very odd to me. Best recommendation: try compiling it and watch what happens. Never hand anything in without trying it.

                  It is by will alone I set my mind in motion. It is by the juice of Coffea arabica that thoughts acquire speed, the table acquires stains, the stains become a warning. It is by will alone I set my mind in motion.

                  Cobalt 1 Reply Last reply Reply Quote 2
                  • Cobalt
                    Cobalt Tutorialist @Chime last edited by

                    @Chime said:

                    Also... the . after println seems very odd to me. Best recommendation: try compiling it and watch what happens. Never hand anything in without trying it.

                    Curious, I don't remember the .being there.. o.O I was pretty tired last night though. You are right the . at the end of the println will mean it won't compile, or won't compile correctly.

                    I'm on to the current question of my worksheet/project for the weekend:

                    • Write a setter method in the class Practice called setNum() which will receive an integer parameter and assign the instance vampire Number to be that value. What is your code?

                    Public void setNumber(int value)
                    {
                          Number = value;
                    }
                    

                    (That is straight from what my instructor told me to do.)

                    Then...

                    • Write lies of code in the main() to ask the user to enter two integer values and get those values for the user. What is your code?

                    int Num1 = Fred.nextInt();
                    System.out.println("You entered: " + Num1);
                    System.out.print("Please enter an integer: ");
                    int Num2 = Fred.nextInt();
                    System.out.println("You entered: " + Num2);
                    

                    (The worksheet has us declaring the Scanner object as Fred.)

                    Then:

                    • Write lines of code in man() to call the method setNumber() for both the object object1 and myObject sending the two integer values you received in #15 above as parameters what is your code?

                    object1.setNumber(Num1);
                    myObject.setNumber(Num2);
                    

                    Aaand then:

                    • Write lines of code in main() to print the two object, so you can see if the values of the instance variables have changed. What is your code? Run the program. Did the values of the instance variables change?

                    This is around the part where I am stuck. The problem:

                    I'm making a Practice class, inside of it needs to be a setNumber() method, I need to stick user-inputted integers into said setNumber(), I then need to feed /that/ through the object1 and myObject objects.

                    I know I need a setter method, and a getterx. But I've gone full on stupid, and just cannot figure out how to connect this things together-- the getter method is driving me nuts.

                    Below is the full code of the two files:

                    Practice Class File

                    public class Practice
                    {	private int Number;
                    	private char Thing;
                    
                    
                    	public	Practice(int Num, char Ans) // constructor
                    	{	Number = Num;
                    		Thing = Ans;
                    	}
                    
                    	public String toString()
                    	{ 	String Result;
                    		Result = "Number is: " + Number + "Thing is: " + Thing;
                    		return Result;
                    	}
                    
                    	public void Story()
                    	{	System.out.println(" This is a cute little method that only prints things");
                    		System.out.println(" First will print the value of the integer: " + Number);
                    		System.out.println(" Now we print the character: " + Thing);
                    		System.out.println(" Number and Thing are called instance variables");
                    		System.out.println(" This is a method called \"Story\" ");
                    		System.out.println(" ... And now we are done:)" );
                    
                    	}
                    
                    	public long SixTimes()
                    	{	long BigNum;
                    		BigNum = Number*Number*Number*Number*Number*Number;
                    		return BigNum;
                    
                    	}
                    
                    	public void setNumber(int value)
                    	{
                    		Number = value;
                    	}
                    
                    	public int getNumber()
                    	{
                    		return Number;
                    	}
                    
                    } // end class
                    

                    Program File

                    import java.util.Scanner;
                    
                    public class Worksheet_4
                    
                    {	public static void main(String[] args)
                    	{	Scanner Fred = new Scanner(System.in);
                    		String Name;
                    		Practice object1 = new Practice (6, '$');
                    		Practice myObject = new Practice(34, '#');
                    		System.out.println("Object 1 is: ");
                    		System.out.println(object1);
                    		System.out.println("Today we practice writing methods ");
                    	/*	object1.Story();
                    		myObject.Story();  ** Removing the calls for Story()*/
                    									// insert code here
                    		long MyNum=object1.SixTimes();
                    		long myObj=myObject.SixTimes();
                    		System.out.println("MyNum is: " + MyNum);
                    		System.out.println("myObj is: " + myObj);
                    		System.out.print("Please enter an integer: ");
                    		int Num1 = Fred.nextInt();
                    		System.out.println("You entered: " + Num1);
                    		System.out.print("Please enter an integer: ");
                    		int Num2 = Fred.nextInt();
                    		System.out.println("You entered: " + Num2);
                    		object1.setNumber(Num1);
                    		myObject.setNumber(Num2);
                    
                    
                    		System.out.println("\n\n\n");
                    	} // end main
                    } // end class
                    

                    Halp internets?

                    1 Reply Last reply Reply Quote 0
                    • Glitch
                      Glitch last edited by

                      Yeah, your dot after the println isn't going to work. It's the function you're calling on out and you want to pass it the lyrics as a string parameter.

                      1 Reply Last reply Reply Quote 0
                      • Cobalt
                        Cobalt Tutorialist last edited by Cobalt

                        • Below the Ranom100() method, write another method called RandomInRange() that accepts two integer parameters. The parameters represent a range of numbers, the first parameter will be smaller than the second. The method will generate and return a random number in the specified range (inclusive). What is the code for your method?

                        Current file:

                        // CS161 Fall 2015				Worksheet #5
                        // Mon/Wed/Fri 8:00am
                        // Date: 10/30/15
                        //
                        // Program Name: Worksheet_5.java
                        // Program Description:
                        //					Worksheet #5 exercises
                        //********************************************************************
                        
                        import java.util.Scanner;
                        import java.util.Random;
                        
                        public class Worksheet_5
                        {	public static void main(String [] args)
                        	{	Scanner scan = new Scanner(System.in);
                        		System.out.println("Today we practice writing methods again.");
                        	/*	System.out.println("Here is the first method: \n");
                        		Lyrics();
                        		System.out.println("Here it is again: \n");
                        		Lyrics(); */
                        
                        	/*	System.out.println(" 2 to the fourth power is: " + FourPower(2));
                        		int MyNum = FourPower(6);
                        		System.out.println(" 6 to the fourth power is: " + MyNum); */
                        		System.out.print("This is a random # between 1 and 100: ");
                        		Random100();
                        
                        		System.out.println("\n\n\n");
                        	} // end method main()
                        
                        	public static void Lyrics()
                        	  {	System.out.println("Row, row, row your boat");
                        	 	System.out.println("Gently down the stream");
                        	 	System.out.println("Life is but a dream.\n\n");
                        	  }	//end method Lyrics()
                        
                        	public static int FourPower(int Number)
                        		{ int Ans = Number * Number * Number * Number;
                        			return Ans;
                        		} 	// end method FourPower()
                        
                        	public static void Random100()
                        		{
                        			Random randObj = new Random();
                        			System.out.println(randObj.nextInt(100) + 1);
                        		}
                        
                        	public static Random RandomInRange(int Low, int High);
                        		{
                        			int Low = value;
                        			int High = value;
                        		}
                        
                        				//add next method here
                        
                        } // end class
                        

                        Brain doesn't work. Don't know what to do. Help.

                        1 Reply Last reply Reply Quote 0
                        • Glitch
                          Glitch last edited by Glitch

                          The first thing I did was look at the docs for Random. The docs for Random.nextInt( int n ) tell us that it can get a range between 0, inclusive, and the passed value, exclusive.

                          The question asks you for low to high, inclusive for both. You could do some number offsetting here with your low and high parameters to get something that would work. (As an example, look at your 1-100 example, by adding 1, you've offset the low and high end by 1).

                          ETA: Trying to help without giving away the answer, if it's not clear enough, though, let me know.

                          Cobalt 1 Reply Last reply Reply Quote 0
                          • Cobalt
                            Cobalt Tutorialist @Glitch last edited by

                            @Glitch I know how to make the high/low inclusive, I don't know how to make them specifiable.

                            1 Reply Last reply Reply Quote 0
                            • Glitch
                              Glitch last edited by Glitch

                              @Cobaltasaurus I assume it means through your scanner?

                              System.out.println("Please select the lower bound: ");
                              int low = scan.nextInt();
                              System.out.println("Please select the upper bound: ");
                              int high = scan.nextInt();
                              System.out.println("Your random number is: " + RandomInRange(low, high));
                              

                              Also, your RandomInRange function should probably return an int and not have a semi-colon at the end,
                              public static int RandomInRange(int low, int high)

                              1 Reply Last reply Reply Quote 0
                              • Cobalt
                                Cobalt Tutorialist last edited by

                                Belatedly: Thanks @Glitch! That helped.

                                I have the current code:

                                import java.util.Scanner;
                                
                                public class Worksheet_7
                                
                                {	public static void main(String[] args)
                                	{	Scanner Fred = new Scanner(System.in);
                                		System.out.println("Today we practice using switch statements \n\n");
                                		char Ans;
                                		char DoMore = 'Y';
                                		while(DoMore =='Y' || DoMore =='y')
                                		{
                                			System.out.println("What kind of a car do you drive?");
                                			System.out.println(" (F)ord, (C)hevy, (B)uick, (M)azda, (H)onda, (S)aab, (V)olkswagen");
                                			String Word = Fred.next();
                                			Word = Word.toUpperCase();
                                			Ans = Word.charAt(0);
                                
                                			while ( !(Ans == 'F'));
                                				
                                			switch(Ans)
                                				{	case 'C' : System.out.println("Chevy - the heartbeat of America");
                                							   System.out.println("Chevy rhymes with heavy, don't you know?");
                                				    		   break;
                                					case 'F': System.out.println("Ford tough");
                                							   System.out.println("Ford rough, if you ask me.");
                                								break;
                                					case 'B': System.out.println("Buick - luxury you can afford.");
                                							   System.out.println("Buick .... ick??");
                                								break;
                                					case 'M': System.out.println("Mazda - Zoom, zoom");
                                							   System.out.println("Madza - sputter, sputter, grk, grrrrinnnding noises.");
                                								break;
                                					case 'H': System.out.println("Honda - Always dependable");
                                							   System.out.println("So why is mine always breaking down?!");
                                								break;
                                					case 'S': System.out.println("Saab - Find your own road");
                                							   System.out.println("You got Saab money for that Saab car you're buying?");
                                								break;
                                					case 'V': System.out.println("Volkswagen - Vee-Dub");
                                								System.out.println("Why is the trunk in the front??");
                                								break;
                                					default: System.out.println("Get a better car...");
                                				} // end switch
                                
                                			System.out.println("Try again? (Y/N)");
                                			Word = Fred.next();
                                			Word = Word.toUpperCase();
                                			DoMore = Word.charAt(0);
                                		}
                                	System.out.println("\n\nWell done for now..");
                                	} // end main
                                }//end class
                                

                                And I need to make it so that if you don't type F,C,V,B, etc. any of those, you'll be prompted to type one of them, but because of how the not operator works I can't get it to work. >.<

                                1 Reply Last reply Reply Quote 0
                                • Glitch
                                  Glitch last edited by

                                  @Cobaltasaurus

                                  The real error I see is that you've closed your while statement with that semi-colon at the end. This means there's nothing going on inside that loop.
                                  while ( !(Ans == 'F'));
                                  You need to make a block like the rest of your code, with the car question inside of the while loop.

                                  while (<letter check logic>) {
                                    System.out.println("What kind of a car do you drive?");
                                    System.out.println(" (F)ord, (C)hevy, (B)uick, (M)azda, (H)onda, (S)aab, (V)olkswagen");
                                    String Word = Fred.next();
                                    Word = Word.toUpperCase();
                                    Ans = Word.charAt(0);
                                  }
                                  

                                  For your letter check logic, you'll learn better ways of doing it, but in keeping with intro level stuff, you want any selection from your list to be true so that you can then use the not operator to reverse it.
                                  !(Ans == 'F' || Ans == 'C' || ...)

                                  Finally, you're already forcing the input to uppercase, so you don't need to check against the lowercase.

                                  I haven't tested any of this code, of course, so this is just an on-paper response.

                                  1 Reply Last reply Reply Quote 0
                                  • Cobalt
                                    Cobalt Tutorialist last edited by

                                    @Glitch said:

                                    @Cobaltasaurus

                                    The real error I see is that you've closed your while statement with that semi-colon at the end. This means there's nothing going on inside that loop.
                                    while ( !(Ans == 'F'));

                                    Damnit! Thanks for catching that, bad habit.

                                    For your letter check logic, you'll learn better ways of doing it, but in keeping with intro level stuff, you want any selection from your list to be true so that you can then use the not operator to reverse it.
                                    !(Ans == 'F' || Ans == 'C' || ...)

                                    I'm superbad and forgot to put in that I had managed to get it work, but it was what you said. Let me put the code in:

                                    import java.util.Scanner;
                                    
                                    public class Worksheet_7
                                    
                                    {	public static void main(String[] args)
                                    	{	Scanner Fred = new Scanner(System.in);
                                    		System.out.println("Today we practice using switch statements \n\n");
                                    		char Ans;
                                    		char DoMore = 'Y';
                                    		while(DoMore =='Y' || DoMore =='y')
                                    		{
                                    			System.out.println("What kind of a car do you drive?");
                                    			System.out.println(" (F)ord, (C)hevy, (B)uick, (M)azda, (H)onda, (S)aab, (V)olkswagen");
                                    			String Word = Fred.next();
                                    			Word = Word.toUpperCase();
                                    			Ans = Word.charAt(0);
                                    
                                    			while( !(Ans =='F' || Ans == 'C' || Ans == 'B' || Ans == 'M' || Ans == 'H' || Ans == 'S' || Ans == 'V'))
                                    			{	System.out.println("What kind of a car do you drive?");
                                    				System.out.println(" (F)ord, (C)hevy, (B)uick, (M)azda, (H)onda, (S)aab, (V)olkswagen");
                                    				Word = Fred.next();
                                    				Word = Word.toUpperCase();
                                    				Ans = Word.charAt(0);
                                    			}
                                    
                                    			switch(Ans)
                                    				{	case 'C' : System.out.println("Chevy - the heartbeat of America");
                                    							   System.out.println("Chevy rhymes with heavy, don't you know?");
                                    				    		   break;
                                    					case 'F': System.out.println("Ford tough");
                                    							   System.out.println("Ford rough, if you ask me.");
                                    								break;
                                    					case 'B': System.out.println("Buick - luxury you can afford.");
                                    							   System.out.println("Buick .... ick??");
                                    								break;
                                    					case 'M': System.out.println("Mazda - Zoom, zoom");
                                    							   System.out.println("Madza - sputter, sputter, grk, grrrrinnnding noises.");
                                    								break;
                                    					case 'H': System.out.println("Honda - Always dependable");
                                    							   System.out.println("So why is mine always breaking down?!");
                                    								break;
                                    					case 'S': System.out.println("Saab - Find your own road");
                                    							   System.out.println("You got Saab money for that Saab car you're buying?");
                                    								break;
                                    					case 'V': System.out.println("Volkswagen - Vee-Dub");
                                    								System.out.println("Why is the trunk in the front??");
                                    								break;
                                    					default: System.out.println("Get a better car...");
                                    				} // end switch
                                    
                                    			System.out.println("Try again? (Y/N)");
                                    			Word = Fred.next();
                                    			Word = Word.toUpperCase();
                                    			DoMore = Word.charAt(0);
                                    		}
                                    	System.out.println("\n\nWell done for now..");
                                    	} // end main
                                    }//end class
                                    

                                    This seemed to work beautifully.

                                    1 Reply Last reply Reply Quote 0
                                    • Cobalt
                                      Cobalt Tutorialist last edited by

                                      @Glitch
                                      Like a month later I finally got RandomInRange working:

                                      Here is the method:

                                      	public static int RandomInRange(int low, int high)
                                      		{
                                      			Random r = new Random();
                                      			int range = (high-low+1);
                                      			int num = r.nextInt(range)+low;
                                      			return num;
                                      		}
                                      

                                      Outside of it there is a scanner asking for the lower bound and the upper bound of the range.

                                      1 Reply Last reply Reply Quote 0
                                      • Cobalt
                                        Cobalt Tutorialist last edited by

                                        Moar Java~ Here is my first lab for this term. I was forced to use switches to do this:

                                        //********************************************************************
                                        // Date: 01/09/16
                                        // Program Name:
                                        // Program Description: This program asks the user to input a number between 50-200
                                        //			this is the number of times a random number between 1-100
                                        //			will be generated.  This program uses a verification of input loop
                                        //			to make sure the user is putting in the right number range.
                                        //			It then stores the number into an array and uses at least one switch.
                                        //			It will finally print a histogram of the numbers showing the frequency
                                        //			Of which each number was randomly generated.
                                        // PseudoCode:
                                        //	Ask user to enter value 50-200 (How Many #?)
                                        //	Verify input between 50-200
                                        // 	Declare int array size 10
                                        //	Repeat (how many times)
                                        //		generate int (1-100)
                                        //		convert num to correct range
                                        //			range =(num-1)/10
                                        //		increment array[range]
                                        //
                                        //	print histogram
                                        //
                                        // ( Test Oracle goes here! )
                                        //********************************************************************
                                        import java.util.Scanner;
                                        import java.util.Random;
                                        
                                        public class Lab_1
                                        { public static void main(String[]args)
                                         {
                                        	// setting up required objects
                                        	Scanner s = new Scanner(System.in);
                                        	Random r = new Random();
                                        	int range[] = new int[10];
                                        	int times = 0, total = 0;
                                        	int value;
                                        
                                        	// Asking the user how many times to generator a random number, at least 50 less than 200.
                                        
                                        	while ( times < 50 | times > 200 )
                                        	{
                                        		System.out.println("How many times should we generate a random number?  Must be between 50-200 times.");
                                        		times = s.nextInt();
                                        	}
                                        
                                        	for (int tracker = 0; tracker < times; tracker++)
                                        	{
                                        		value = r.nextInt(100)+1;
                                        		value = (value-1)/10;
                                        
                                        		switch(value)
                                        		{
                                        			case 0: range[0]++;
                                        					break;
                                        			case 1: range[1]++;
                                        					break;
                                        			case 2: range[2]++;
                                        					break;
                                        			case 3: range[3]++;
                                        					break;
                                        			case 4: range[4]++;
                                        					break;
                                        			case 5: range[5]++;
                                        					break;
                                        			case 6: range[6]++;
                                        					break;
                                        			case 7: range[7]++;
                                        					break;
                                        			case 8: range[8]++;
                                        					break;
                                        			case 9: range[9]++;
                                        					break;
                                        			default: System.out.println("Ooops.");
                                        		}
                                        	}
                                        
                                        	System.out.println("---------------------------------------------------------------");
                                        
                                        	for (int arraysub=0; arraysub < range.length; arraysub++)
                                        	{
                                        		switch(arraysub)
                                        		{
                                        			case 0: System.out.print("|  01-10\t|");
                                        					break;
                                        			case 1: System.out.print("|  11-20\t|");
                                        					break;
                                        			case 2: System.out.print("|  21-30\t|");
                                        					break;
                                        			case 3: System.out.print("|  31-40\t|");
                                        					break;
                                        			case 4: System.out.print("|  41-50\t|");
                                        					break;
                                        			case 5: System.out.print("|  51-60\t|");
                                        					break;
                                        			case 6: System.out.print("|  61-70\t|");
                                        					break;
                                        			case 7: System.out.print("|  71-80\t|");
                                        					break;
                                        			case 8: System.out.print("|  81-90\t|");
                                        					break;
                                        			case 9: System.out.print("|  91-100\t|");
                                        					break;
                                        			default: System.out.print("|  ??-??\t|");
                                        		}
                                        		for (int dots = 0; dots < range[arraysub]; dots++)
                                        		{
                                        			System.out.print("*");
                                        		}
                                        
                                        		System.out.println();
                                        		total = total + range[arraysub]; // checking to make sure the right amount of numbers are generated
                                        	}
                                        
                                        	System.out.println("---------------------------------------------------------------");
                                        	System.out.println(total + " numbers generated");
                                        
                                        
                                         } // end main
                                        } // end class
                                        
                                        1 Reply Last reply Reply Quote 0
                                        • 1 / 1
                                        • First post
                                          Last post