Using classes and objects
- Where to go from here
Viewers: in countries Watching now:
Finally, the course compares how code is written in several different languages, the libraries and frameworks that have grown around them, and the reasons to choose each one.
- Writing source code
- Understanding compiled and interpreted languages
- Requesting input
- Working with numbers, characters, strings, and operators
- Writing conditional code
- Making the code modular
- Writing loops
- Finding patterns in strings
- Working with arrays and collections
- Adopting a programming style
- Reading and writing to various locations
- Managing memory usage
- Learning about other languages
Using classes and objects
Let's say var today = new Date. The variable today would now contain today's date, not as a string but as a Date object. We can create new Date objects with a particular data. And actually because this object definition stores time, we could even create a new Date object with a year, month, day, hours, minutes, seconds in it. Now you might think, but so what? Well, of course the great thing is these are objects. They have properties, and they have methods. We can start using the name of one of these variables like today or y2k and calling parts of it.
We can say today.getMonth and it would return 0-11 for the month. today.getFullYear returns the normal year. .getDate, .getDay, .getHours, and so on. So there is a lot of behavior that the Date object can give us to start pulling apart dates. And not only that, but they have the flip side of the get method, which are our set methods. We can create a new Date object and then use it to say .setMonth or .setFullYear or .setDay.
What you might do is first say create a variable called x and I am setting this to 200.6. And I would like to round this number. This is the way I do it. I would create a new variable called y and create it by calling Math.round, passing in that variable x. That would give me 201. It would round it up. So we often just use the Math object directly. Say we create several variables. One is 200, one is 10000, one is 4, but at some point in my program, I don't know which one is which and which one is higher.
I can create a new variable called biggest, which will be whatever gets returned from calling Math.max and passing in those three numbers. In fact, Math.max will take as many numbers as you want to pass into it and always return the biggest one. If we have a .max, it would be nice if we had a .min. And not surprisingly, we do. So the Math object is there and has many built-in methods and properties for giving us Pi, calculating random numbers, rounding things up or down, calculating logarithms, sines and cosines and so on.
Find answers to the most frequently asked questions about Foundations of Programming: Fundamentals .
Here are the FAQs that matched your search "" :
- Q: Using TextEdit with Mac OS 10.9 Mavericks?
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.