Skip to content

Programming by Design

If you're not prepared to be wrong, you'll never come up with anything original. – Sir Ken Robinson

  • About
  • Java-PbD
  • C-PbD
  • ASM-PbD
  • Algorithms
  • Other

Useful CISS-110 Stuff

Posted on February 15, 2016March 25, 2020 By William Jojo
Other
public class FunWithArrays {
  
  public static double calcAverage (int a, int b, int c, int d, int e, int f, int g, int h, int i, int j ) {
    
    return (a + b + c + d + e + f + g + h + i + j) / 10.0;
    
  }
  
  public static double calcAverage (int[] ia) {
    
    int x, sum = 0;
    
    for ( x = 0; x < ia.length; x++ )
      sum = sum + ia[x];
    
    return sum / (double)ia.length;
  }
  
  public static double calcAverage (int[] ia, int n) {
    
    int x, sum = 0;
    
    if ( n > ia.length)
      return -1.0;
    
    for ( x = 0; x < n; x++ )
      sum = sum + ia[x];
    
    return sum / (double)n;
  }
  
  
  public static void main (String[] args) {
    
    int x;
    int[] a = new int[10];
    
    // Horrible test of horrible first version!
    System.out.println(calcAverage(10,20,30,40,50,60,70,80,90,100));
    
    // Generate ramdom test grades in the inclusive range 60 - 100 
    for ( x = 0; x < a.length; x++)
      a[x] = (int)(Math.random() * 41 + 60);
    
    // Show me the test grades
    for ( x = 0; x < a.length; x++)
      System.out.println("a[" + x + "] = " + a[x]);
    
    System.out.println("The average of the whole array is " + calcAverage(a));
    
    System.out.println("The average of the first 3 elements is " + calcAverage(a, 3));
    
    System.out.println("This should be -1.0. Is it? " + calcAverage(a, 11));
  }
  
}

public class testPerson {
  
  public static void main (String[] args) {
    
    
    Person me, you;
    
    me = new Person("Bill", "Jojo", 50);
    you = new Person();
    
    you.setGivenName("Other");
    you.setSurname("Person");
    you.setAge(35);
    
    
    System.out.println("me.getGivenName() = " + me.getGivenName());
    System.out.println("me.getSurname() = " + me.getSurname());
    System.out.println("me.getAge() = " + me.getAge() + "\n");

    
    System.out.println("you.getGivenName() = " + you.getGivenName());
    System.out.println("you.getSurname() = " + you.getSurname());
    System.out.println("you.getAge() = " + you.getAge() + "\n");
    
    // This calls toString()!
    System.out.println(me);
    System.out.println(you);
  }
  
}

Converting Strings and Numbers (SSN Style)

public class Ssn {
  
  public static void main (String[] args) {

    String stringssn;
    int ssn, f, m, l;
    
    ssn = 123456789;
    // ssn = 123050067; // testing with zeroes
    
    f = ssn / 1000000; // 123
    ssn = ssn % 1000000; // 456789
    m = ssn / 10000; // 45
    l = ssn % 10000; // 6789
    System.out.println("f = " + f);
    System.out.println("m = " + m);
    System.out.println("l = " + l);
    
    System.out.printf("%03d-%02d-%04d%n", f, m ,l);
    stringssn = String.format("%03d-%02d-%04d%n", f, m ,l);
    System.out.printf(stringssn);
  }
}

Example 2: SSN formatting using integers.

public class Ssn2 {
  
  public static void main (String[] args) {
    // 023050089
    
    String ssn = "023050089", stringssn;
    String f, m, l;
    
    f = ssn.substring(0,3);
    m = ssn.substring(3,5);
    l = ssn.substring(5);
    
    System.out.println("f = " + f);
    System.out.println("m = " + m);
    System.out.println("l = " + l);
    
    stringssn = f + "-" + m + "-" + l;
    System.out.println(stringssn);
  }
}

Example 3: SSN formatting using String.


Fun Scanner stuff:

import java.util.Scanner;

public class ScannerStuff
{
  
  public static Scanner kb = new Scanner(System.in);
  
  public static void main (String[] args)
  {
 
    int x, y;
    double d;
    
    System.out.print("Enter a value for x: ");
    x = kb.nextInt();
    
    System.out.print("Enter a value for y: ");
    y = kb.nextInt();
    
    //System.out.print("Enter a value for d: ");
    //d = kb.nextDouble();
    
    System.out.println("x is " + x);
    System.out.println("y is " + y);
    
    System.out.println(x + " to the " + y + " is " + Math.pow(x,y));
  }
  
}

Fun conditional stuff:

import java.util.Scanner;

public class Conditional {
  
  static Scanner kb = new Scanner(System.in);
  
  public static void main(String[] args) {
    
    int i;
    
    
    System.out.println("Enter an integer: ");
    i = kb.nextInt();
    
    if ( i < 0 ) {
      System.out.println(i + " is negative.");
      System.out.println("Yep, that's right...");
    } else if ( i > 0 )
      System.out.println(i + " is positive.");
    else
      System.out.println(i + " is zero.");
    
    if ( i > 0 ) 
      System.out.println(i + " is positive.");
    else if ( i < 0 )
      System.out.println(i + " is negative.");
    else
      System.out.println(i + " is zero.");
  }
}

Fun iterative stuff:

import java.util.Scanner;

public class Conditional2 {
  
  static Scanner kb = new Scanner(System.in);
  
  public static void main(String[] args) {
    
    int x;
    

    /* while loop to do even/odd */
    x = 0;
    while ( x <= 10 ) {
      if ( x % 2 == 0 )
        System.out.println(x + " is an Even number!");
      else
        System.out.println(x + " is an Odd number!");
      
      x++;
    }
    
    System.out.println("The value of x is " + x);
    
    /* for loop to do the same */
    for (x = 0 ; x <= 10; x++)
      if ( x % 2 == 0 )
      System.out.println(x + " is an Even number!");
    else
      System.out.println(x + " is an Odd number!");
    
    System.out.println("The value of x is " + x);
  }
}

EOF stuff

import java.util.Scanner;

public class Eof {
  
  static Scanner kb = new Scanner(System.in);
  
  public static void main(String[] args) {
    
    String line;
    int x;
    
    System.out.print("Enter a string (CTRL-D to end): ");
    while ( kb.hasNext() )
    {
      x = kb.nextInt();  // read the line
      //System.out.println("Input was " + line.length() + " characters long.");
      System.out.println("Input was " + x);
      System.out.print("Enter a string (CTRL-D to end): ");
    }
  }
}

This is just a bad idea... Using Strings for ints while expecting an English response that we are done...
Just bad...

import java.util.Scanner;

public class Obtuse {
  
  static Scanner kb = new Scanner(System.in);
  
  public static void main(String[] args) {
    
    String line;
    int x;
    
    System.out.print("Enter an integer (enter \"done\" when complete): ");
    line = kb.nextLine();
    //while ( ! line.equals("done") )
    while ( line.compareToIgnoreCase("done") != 0)
    {
      x = Integer.parseInt(line);
      System.out.println("Input was " + x + ".");
      System.out.print("Enter an integer (enter \"done\" when complete): ");
      line = kb.nextLine();
    }
  }
}

EOF look (like above) but with double and printf.

import java.util.Scanner;

public class Printf {
  
  static Scanner kb = new Scanner(System.in);
  
  public static void main(String[] args) {
    
    double x;
    
    System.out.print("Enter an double (CTRL-D to end): ");
    while ( kb.hasNext() )
    {
      x = kb.nextDouble();  // read the line
      
      System.out.printf("Input to two decimal places is %.2f%n", x);
      System.out.print("Enter a double (CTRL-D to end): ");
    }
  }
}

Fun with floating point numbers...

public class Broken {
  
  public static void main(String[] args) {
    
    
    int x;
    double f = 0;
    
    for ( x = 1; x <= 20; x++) {
      f = f + 0.1;
      System.out.println(f);
      
    }
  }
}

Fun with methods...

public class MoreMethods 
{ 
  public static int sum(int a, int b) {
    
    return a + b;
  }
  
  public static int product (int a, int b) {
    
    return a * b;
  }
  
  public static void main(String[] args) {
    
    int x, y, s;
    
    x = 10;
    y = 15;
    
    System.out.println("The sum     of x and y is " + sum(x,y));
    System.out.println("The product of x and y is " + product(x,y));
  }
  
}

Oh no! Even more fun with methods!

public class MyMethods {
  
  public static void main(String[] args) {
    
    char c;
    
    c = ';';
    if ( myIsLetter(c) )
      System.out.println(c + " is a letter.");
    if ( myIsDigit(c) )
      System.out.println(c + " is a digit.");
    if ( myIsLetterOrDigit(c) )
      System.out.println(c + " is a letter or digit.");
    else
      System.out.println(c + " is not a letter or digit.");
    if ( myIsLowerCase(c) )
      System.out.println(c + " is lower case.");
    if ( myIsUpperCase(c) )
      System.out.println(c + " is upper case.");   
    
  }
  
  public static boolean myIsDigit(char ch) {
    
    return ch >= '0' && ch <= '9';
      
  }
  
  public static boolean myIsLetter(char ch) {
    
    return myIsUpperCase(ch) || myIsLowerCase(ch);
    
  }
  
  public static boolean myIsLetterOrDigit(char ch) {
    
    return myIsLetter(ch) || myIsDigit(ch);
  }
  
  public static boolean myIsUpperCase(char ch) {
    
    return  ch >= 'A' && ch <= 'Z';
      
  }
  
  public static boolean myIsLowerCase(char ch) {
    
    return ( ch >= 'a' && ch <= 'z' );
    
  }
}

Loops and strings and things...

import java.util.Scanner;

public class string {
  
  public static Scanner kb = new Scanner(System.in);
  
  public static void dumpString(String str) {
    for ( int x = 0; x < str.length(); x++)
      System.out.println("str.charAt(" + x + ") is \"" + str.charAt(x) + "\".");
  }
  
  
  public static void main(String[] args) {
    
    String s = "This is a string.";
    
    System.out.print("Enter a string: ");
    while (kb.hasNext()) {
      s = kb.nextLine();
      dumpString(s);
      System.out.print("Enter a string: ");
    }
  }
}

Post navigation

❮ Previous Post: Useful CISS-111 Stuff
Next Post: Network Details for CISS-150 ❯

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Copyright © 2018 – 2025 Programming by Design.