Category Archives: Java

Sorting an Array in Java

This short little program quickly sorts a pre-filled array in both ascending and descending order and displays the sorted array to the user. This can be altered so that a user can enter the numbers into the array to be sorted.

This was created and run in Eclipse.
public class SortArray {

 public static void main(String[] args) {
    int a[] = {13,5,1985,10,31};
  
  System.out.println(“Ascending Order: “);
  for(int i = 0; i<5; i++)
  {
   for(int j = i+1; j<5; j++)
   {
    if(a[i]>a[j])
    {
     int temp = a[i];
     a[i] = a[j];
     a[j] = temp;
    }
   }
  }
  for(int i = 0; i<5; i++)
  {
   System.out.println(+a[i]);
  }
  System.out.println(“Descending Order: “);
  for(int i = 0; i<5; i++)
  {
   for(int j = i+1; j<5; j++)
   {
    if(a[i]<a[j])
    {
     int temp = a[i];
     a[i] = a[j];
     a[j] = temp;
    }
   }
  }
  for(int i = 0; i<5; i++)
  {
   System.out.println(+a[i]);
  }
 }

}

Armstrong Numbers in Java

This is a cool program that displays all of the Armstrong Numbers between 100 and 1000. It can be easily altered to take boundaries from a user and run the program from the suggested lower and upper bounds. 

As an aside, I find it amusing that these numbers are also called Narcissistic Numbers, and are part of a branch of mathematics called ‘Recreational Number Theory’ – all I can picture are a bunch of mathematicians lounging on a beach doing math puzzles!

This program was written in Java and executed in Eclipse.

import java.lang.Math; // Import to use the power expression

public class Armstrong
{
 public static void main(String[] args)
 {
  int i, a, b, c, sum=0;
  
  for(i=100; i<=999; i++) /* The upper limit is set to 999 because this code handles three digit numbers, not four! If you entered 1000, then 1000 would show up in the results, and 1000 is not an Armstrong Number*/
  {
   c=i;  // Assigns the counter value to c
   a=c%10;  // Performs modulo division to remove the last digit from the number to save for later
   c=c/10;  // This operation leaves the remaining two digits of the number
   b=c%10;  // Performs modulo division to break up the last two digits and  saves the middle digit for later
   c=c/10;  // This operation leaves the last number, which should be the starting digit of the entire number
   
   sum=(int)Math.pow(a, 3) + (int)Math.pow(b, 3) + (int)Math.pow(c, 3); // Adds each cubed number from the above operations
   
   if(i==sum) // This loop compares the initial counter value to the end result (‘sum’)
    System.out.println(i);  // The value is printed if the two numbers are identical
  }
 }
}
//——————————————————————————-//
//
// Example of how this math works:
// If c = 100, then:
// 100 MOD 10 = 0 (The last digit of c)
// 100/10 = 10
// 10 MOD 10 = 0 (The middle digit of c)
// 10/10 = 1 (The first digit of c)
// Now that the number has been broken up, math can be performed on the individual numbers while the whole remains intact

Quadratic Formula in Java

This is a quick and simple program that finds the two roots of the quadratic formula. This could be modified so that the user could plug in all of the variables if needed. This was created in Eclipse for Java.

import java.lang.Math;

public class Stuff {

 public static void main(String[] args) {
  
  double a = 1.0;
  double b = 3.0;
  double c = 2.0;
  double x1, x2;
  double d = Math.sqrt((b*b) – (4*a*c));
  
  x1 = (-b + d) / (2 * a);
  x2 = (-b – d) / (2 * a);
  
  System.out.println(“The first root of the forumal is: ” +x1);
  System.out.println(“The second root of the formula is: ” +x2);
  
 }

}

Whenever you use math functions in Java (or any language, for that matter), it is imperative that you import that library in the header area; otherwise, the compiler will not recognize ‘pow’, ‘sqrt’, etc.

Pizza!

This program uses Java classes to show some basic data regarding, in this case, pizza sizes, toppings and prices. A bit simplistic, but it demonstrates some classic Java commands. This was created and executed using Eclipse.

public class Week3
{
 public static void main(String[] args)
 {
    Pizza2 p1 = new Pizza2();
    Pizza2 p2 = new Pizza2();
    int Size;
    String Toppings;
    double Price;

    p1.setToppings(“Cheese”);
    p1.setSize(16);
    p1.setPrice(13.99);

    Toppings = p1.getToppings();
    Size = p1.getSize();
    Price = p1.getPrice();

    System.out.println(“Pizza has ” + Toppings + ” and the size is ” + Size  + ” inches ” + ” and the price is ” + Price);
 
    p2.setToppings(“Pepperoni”);
    p2.setSize(12);
    p2.setPrice(11.99);

    Toppings = p2.getToppings();
    Size = p2.getSize();
    Price = p2.getPrice();

    System.out.println(“Pizza has ” + Toppings + ” and the size is ” + Size  + ” inches ” + ” and the price is ” + Price);

 }
}
public class Pizza2
{
  int Size;
  String Toppings;
  double Price;

  public void setToppings(String t)
  {
     Toppings = t;
  }

  public void setSize(int pSize)
  {  
     Size = pSize;
  }

  public void setPrice(double pPrice)
  {
     Price = pPrice;
  }

  public String getToppings()
  {
     return(Toppings);
  }

  public int getSize()
  {
     return(Size);
  }
  
  public double getPrice()
  {
   return(Price);
  }
  
}

Screenshot: