Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎notes‎ > ‎

2017-11-09 HW

posted Nov 9, 2017, 10:07 AM by Konstantinovich Samuel   [ updated Nov 9, 2017, 10:16 AM ]
Goal: Terminal arguments, Files



Terminal commands often have arguments!

Here is how we access them:

//for easy printing of arrays
import java.util.Arrays;
public class Demo1{
  public static void main(String[]args){
    //args has your terminal options
    System.out.println(Arrays.toString(args));
  }
}


Now call your program on the terminal:

java Demo1 fish 123 abba good day

This is like saying: 
    args = {"fish", "123", "abba", "good", "day"};


This can be used to pass values into the program:

public class Demo1b{
  public static void main(String[]args){
    int defaultSize = 10;    

    //get a value from the arguments
    if(args.length > 0){
        defaultSize = Integer.parseInt(args[0]);
    }
    
    //DO STUFF WITH defaultSize

  }  
}






Now let us look at files:

//for file stuff:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Demo2{
  public static void main(String[]args){
    String fileName = "file.txt";
    try{
      File f = new File(fileName);//can combine
      Scanner in = new Scanner(f);//into one line
      
        //NOW read the file here...
      
    }catch(FileNotFoundException e){
      System.out.println("File not found: " + fileName);
      //e.printStackTrace();
      System.exit(1);
    }

    //How can we use the file here? Where should you put it?
  }
}




Reading the file line by line:

while(in.hasNext()){
        String line = in.nextLine();
        //do something with line
        System.out.println(line);
}

Reading the file word by word:

while(in.hasNext()){
        String word = in.next();
        //do something with word
        System.out.println(word);
}

The java Scanner is super useful! It is an easy way to parse through files.





Now let us look at Random:

import java.util.Random;
public class DemoRandom{
  public static void main(String[]args){
    Random rng = new Random();
    for(int i =  0; i < 5; i++ ){
      System.out.println(rng.nextInt() % 1000);
    }
    System.out.println();
    rng = new Random(100);//seed of 100 is stored.
    for(int i =  0; i < 5; i++ ){
      System.out.println(rng.nextInt() % 1000);
    }
  }
}



Homework:   

   /**Attempts to add a given word to the specified position of the WordGrid.
     *The word is added from top left to bottom right, must fit on the WordGrid,
     *and must have a corresponding letter to match any letters that it overlaps.
     *
     *@param word is any text to be added to the word grid.
     *@param row is the vertical locaiton of where you want the word to start.
     *@param col is the horizontal location of where you want the word to start.
     *@return true when the word is added successfully. When the word doesn't fit,
     *or there are overlapping letters that do not match, then false is returned.
     */
    public boolean addWordDiagonal(String word,int row, int col){
    }

-How are Horizontal/Vertical/Diagonal the same/different?
-How can you combine these methods into a much more flexible method?

Comments