Thursday, July 3, 2014

FIrst Java Examples

Here is the code for the first two days:

First Java

package com.spconger.FirstJava;

import java.util.Scanner;

public class Program {
 /*
  * This class takes a word and a number
  * as arguments 
  * and outputs the word as many times as 
  * the number indicates
  * Steve Conger 6/30/2014
  */

 public static void main(String[] args) {
  //initialize the program class
  Program p = new Program();
  p.getInput();

 }
 
 private void getInput(){
  Scanner scan = new Scanner(System.in);
  String name=null;
  int number;
  System.out.println("Please enter a name or something");
  name=scan.next();
 
 
  
  if(name != null)
  {
   System.out.println("Enter how many time you want it to repeat");
   number=scan.nextInt();
   
   display(name, number);
   
  }
  
  else
  {
  
  System.out.println("You must enter a word");
  }
 }
 
 private void display(String name, int number){
  for (int i = 0; i < number; i++){
   System.out.println(name);
  }
 }

}


Array Example

package com.spconger.ArrayExample;

import java.util.Random;
import java.util.Scanner;

public class Program {
 int[] myArray = new int[10];
 
 public static void main(String[] args) {
  Program p = new Program();
  //p.createArray();
  //p.displayArray();
  p.twoDimensionalArray();
 }
 
 private void createArray()
 {
  
  
  Random rand = new Random();
  
  for (int i=0;i<myArray.length;i++){
   myArray[i]=rand.nextInt(300);
  }
 }
 
 private void displayArray(){
  for(int i=0;i<myArray.length;i++){
   System.out.println(myArray[i]);
  }
 }
 
 private void twoDimensionalArray(){
  String[] [] classes = new String[4][2];
  classes[0][0]="ITC115";
  classes[0][1]="Conger";
  classes[1][0]="ITC 240";
  classes[1][1]="Newman";
  classes[2][0]="NET 120";
  classes[2][1]="Messerly";
  classes[3][0]="ITC 224";
  classes[3][1]="Conger";
  
  System.out.println("Enter an instructor's name");
  Scanner scan = new Scanner(System.in);
  String instructor=scan.next();
  
  for(int i=0;i<classes.length;i++){
   if(classes[i][1].equals(instructor)){
    System.out.println(classes[i][0]);
   }
  }
 }
 

}


While Loop Example

package com.spconger.WhileLoopExample;

import java.util.Scanner;

public class Program {

 public static void main(String[] args) {
  Program p = new Program();
  //p.whileLoop();
  p.forEachLoop();

 }
 
 private void whileLoop(){
  String answer="Yes";
  int counter=0;
  Scanner scan = new Scanner(System.in);
  while (answer.equals("Yes") || answer.equals("yes")){
   counter ++;
   System.out.println(counter);
   System.out.println("Would you like to continue? Yes/No");
   
   answer = scan.next();
   
  }
  
  
 }
 private void DoWhileLoop()
 {
   String answer = "no";
   int counter=0;
   Scanner scan = new Scanner(System.in);
   do
   {
    counter++;
    System.out.println(counter);
    System.out.println("Would you like to continue? Yes/No");
   }while(answer.equals("Yes")|| answer.equals("yes"));
 }
 
 private void forEachLoop(){
  String[] colors = new String[] {"Red", "Green", "Blue", "Orange","Purple"};
  for(String s: colors){
   System.out.println(s);
  }
 }

}



No comments:

Post a Comment