One of the more fundamental concepts to uderstand in programming is in finding the maximum or minumum in an array. Suppose you want to find the largest number in an array. Keep a candidate for the maximum. If you find an element with a larger value, then replace the candidate with that value. When you have reached the end of the array list, you have found the maximum. There is just one problem. When you visit the beginning of the array, you don’t yet have a candidate for the maximum. One way to overcome that is to set the candidate to the starting element of the array and start the comparison with the next element.
1 2 3 4 5 6 7 8 9 10 11 | //creating an array with 5 numbers int[] numbers = {1, 3, 5, 7, 9}; //creating a variable and setting it to array position 0 int largestYet = numbers[0] //looping through the rest of the array to find maximum num for(int i = 0; i < numbers.lenght; i++) { int a = numbers[i]; if(a > largestYet) largestYet = a; } |
Inheritance: is a mechanism for enhancing existing classes. If you need to implement a new class and a class representing a more general concept is already available, then the new class can inherit from the existing class. For example , suppose you need to define a class SavingsAccount to model an account that pays a fixed interest rate on deposit. You already have a class BankAccount, and a savings account is a special case of a bank account. In this case, it makes sense to use the language construct of inheritance. Here’s the syntax for the class definition. Read the rest of this entry »
Since this days I’m spending lots of time doing programming in Java, I decided to make this page as a central repository of the more basic concepts in Java programming language that I will be updating over time.
Q: What is an object?
A: Objects are entities in your program that you manipulate by calling methods.
Q: What is a class?
A: A class defines the methods that you can apply to an object.
Q:What is a method?
A: A Java method is a set of Java statements which can be included inside a Java class.
Q: What is a constructor?
A: The process of creating a new object is called a construction. Read the rest of this entry »
Here’s a little Java program that calculates the Acceptable Dating Age Range, based on this comics http://www.xkcd.com/314/ which basically says that as you get olderĀ the dateable age range gets wider based on this formula DateableRange = ( Age/2 ) + 7
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package testproject; /** * * @author jorge */ import java.util.*; public class DatingRange { public static void main(String[] args) { // TODO code application logic here Scanner Age = new Scanner (System.in); //asking the user for age System.out.println("How old are you?"); int UserAge = Age.nextInt(); //calculating acceptable range int AcceptableRange = (UserAge/2)+ 7; //calculating Difference int Diff = UserAge - AcceptableRange; //calculating the oldest you can date int oldest = UserAge + Diff; //output information to user System.out.println("You are "+UserAge+" years old!"); System.out.println("The youngest you can date is "+AcceptableRange+" years old!"); System.out.println("The oldest you can date is "+oldest+" years old!"); } } |