Using predefined functions

From the course: Learning Java 8

  • Course details

    Java is one of the top-five programming languages, and is used for websites, embedded controllers, and Android app development. This is an introduction to get you started programming with Java. Peggy Fisher introduces the basics: data types, strings, arrays, expressions, loops, and functions. She'll help you control the flow and logic of your code, and create classes using the principles of object-oriented design. Then go a bit beyond the basics and learn advanced techniques for working with arrays, manipulating files, and building graphical user interfaces (GUIs) that respond to user input. This three-hour capsule course is perfect for developers who need to get up to speed with Java fast, as well as for beginning programmers who want their first taste of this popular language.


    • Click here to view Peggy Fisher’s instructor page

      Peggy Fisher

      Content Strategist, Software Development Languages at Linkedin Learning Solutions with content

      Peggy Fisher is a programmer and content manager at LinkedIn Learning.

      Peggy's main focus is application programming in Java, Arduino, and C++. She has also worked on courses in COBOL and discrete ,athematics. Previously she was a faculty member at Penn State University's College of Information Sciences and Technology. She started out as a programmer working for a large insurance company, but after 18 years she left her job as a director of information technology to pursue her true passion teaching. She earned a master's degree in math education, and went on to teach high school math and computer science in Pennsylvania. In 2012, Peggy accepted a position as an instructional designer at Penn State, and shortly thereafter began teaching Intro to Application Programming with Java. She is a strong supporter of women in STEM. As one of the few female programming teachers, she serves as a mentor to incoming female freshmen who are considering a career in programming. She was also the K–12 outreach coordinator for the college, where she scheduled, ran, and taught summer camps for middle school and high school students. In a PBS NewsHour interview, she expressed that all students should take at least one programming class either in high school or college. Peggy enjoys constantly learning and finding new and exciting ways to bring technology to life in and outside of the classroom, such as using Arduino microcontrollers or Lego Mindstorms, to help make learning hands-on and fun.

    Skills covered in this course

  • Welcome

    - The Java API or application programming interface is a collection of software packages that programmers can use for graphics, user interface, networking, sound, database, math, and much more. It contains many methods that have already been written and tested. In order to use these specific methods in our program we have to import the necessary libraries by adding them to our program. Let's start by importing the random class. I've created a random numbers project. On line 7, I'm going to import the random class. The random class is part of the Java.util library. By combining these libraries and are now into loops we can start to write program simulations. How about if we simulate rolling two dice one hundred times and count how many times we roll doubles? In this program we will use a random number generator to help with our simulation. We'll need to count how many times we roll doubles. So, let's start by adding some variables. I'll start by adding an integer variable to represent the value for die1. I'll have another variable for die2 and finally, a counter that specifies how many times I get doubles. I'm gonna start the countDoubles variable out at 0. Since I haven't started yet, I don't have any doubles. Next, I need to create an instance of the random class. Similar to what we did with the scanner class I'm gonna type in the name of the class which is Random. I'm gonna give it a variable name. I'm gonna use rand to represent my random number generator, is equal to a new Random a () and a ;. I now have an object, rand, that I can use to generate random numbers. When we wanna specify a range of possible numbers we specify one more than the upper bounds and we get numbers from 0 to that value. Since we don't want to include 0 in our case because a die starts with the number 1 and ends with the number 6, we're going to change our program to include a random number generator that has an upper balance of 6 which will give us 0 to 5 and then we're gonna add 1 so we can get the values 1 to 6. We decided to simulate the die by rolling them one hundred times. So, remember, what kind of loop do we use when we know how many times you wanna execute the code? I hope you said a for loop. So, let's go ahead and do a for loop. So, for, now I need a variable that's gonna be my control variable so I'll do int i = 0. In order to keep going a hundred times my conditions says as long as i is < 100. The last part the for loop syntax is the update. So, I wanna add 1 to i every time. So, I do i++. This will execute this loop from 0 to 99 which was 100 times. Now, I indicate the block of the code that should be executed with curly brackets. Inside my for loop, I'm going to declare die1 and die2 to be a random number between 1 and 6. So, let's do die 1 first is equal to a call to the random number generator.nextInt 'cause I wanna make sure that I get an integer. I'm gonna put the number 6 in parentheses. This'll give me the numbers 0 to 5 but then if I add 1 to each number I now get a range of numbers between 1 and 6. Let's do die2 the same way. Now that I have two random numbers I want to compare them so we're gonna use a decision statement which is an if statement and we'll compare and we're gonna say if dot he value of die1 is equal to remember to compare two values you have to use the equals equals. So, if die1 equals equals die2 meaning that we roll the same value then I am going to add one to my counter variable. countDoubles++. ++ is an abbreviation for adding 1 to the value of that variable. If the two die are not equal, I don't wanna do anything so I'm just gonna go back up to the top of my loop. Okay, when I'm all done I wanna print out the number of times that I got doubles. So, outside the loop, so if I click here I can see the start and stop of my for loop. It looks like line 29 is outside of my for loop 'cause I only want to print the total of number of doubles one time. I'm gonna do System.out.printlin and I'll say I rolled. I wanna dd the number of doubles that I rolled, countDoubles+ again and I'll put the word doubles. So, when I run the program it'll replace countDoubles with a number and it'll say I rolled fifty doubles or whatever the number is. Okay, I think we're ready to test this. I'm gonna go ahead and run it. It looks like the first time through I got 19 doubles. The way that we have it set up we can run it again without changing anything and because it randomly generates each die I should get a different number. This time I got 13. Now, I got 18. As you can see, the number of doubles ranges somewhere between 13 and 18 so far but the numbers could change if I keep testing. The random number class is included in the Java.util.random library. For more information, refer to the Oracle Java doc for detailed information on all the predefined methods available for the random class.

  • Practice while you learn with exercise files

    Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.

    Download the exercise files for this course. Get started with a free trial today.

  • Download courses and learn on the go

    Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.

    Watch this course anytime, anywhere. Get started with a free trial today.