James Liang - ICS Blog

ICS4U Blogging

Intro to Java - Learning Station

INTRO TO JAVA

Station 1: Primitive vs Non-Primitive

AS A GROUP, Create a Venn Diagram to illustrates a few (3-4) similarities and differences between a Primitive and Non-Primitive data type. What is one important thing you learned about primitive vs non-primitive? Why is it important?

One important thing I learned about primitive vs non-primitive was that casing matters. This is important as primitive data types such as float and int MUST be all lower case including the first letter. If it isn’t, the code won’t function as expected. It is the same with non-primitives except the first letter must be uppercase or it won’t function as expected as well. I must take this into consideration in the future as forgetting will result in my code not functioning.

Station 2: Construct the Code

AS A GROUP, construct the code to match the output provided What was key to helping YOU to solve the program? What gave you some difficulty? Why?

The key thing that helped me solve the program was doing it line by line. At first I was overwhelmed by the vast amount of options, but when I started going down the output and figuring out what I needed, it went by much more smoothly. The thing that gave me difficulty was the cluttered mess that the options were first in. It was a challenge to find the right snips of code that I needed as there were just too many options. I know now that I should organize my code as it will become very confusing if it is everywhere like the program was at first. By solving it, I can now visually see what the program is supposed at a glance. Though my code might not be as messy as this, I should still strive to keep it organized for future me and anyone else reading my code.

Station 3: Math Prediction

INDIVIDUALLY, Predict the code on the following slides.

AS A GROUP, compare your answers and sort out any differences. Which part gave you trouble? Why? Add any additional reflections on what you learned in this station.

The part that gave me trouble was the num++ and the ++num. I recalled from my previous Java experience that the order of the ++ mattered; however, I forgot what led to what result. I concluded that it would make sense for the addition to happen afterwards if the ++ was after the num so I went with that result. Another part that gave me trouble was the 5th question with the %. I remembered that it would output the remainder, but I haven’t done long term division in a long time. I then realised that you can’t divide 2 by 10 and end up with a whole number, so the remainder was 2. The final thing I learned in this station was how -= and *= would modify the variable. I was deciding between the modified value reverting back to its original value or setting the new value. I decided that it would make most sense to set the new value, so I used that as the result. I know that -= and *= don’t return to their original value after being used. This can help shorten my code by removing redundant lines that could be replaced with these operators.

Station 4: Predict the Fancy Output

INDIVIDUALLY, predict the code on the following slides.

AS A GROUP, compare your answers and sort out any differences. When you predict code, what strategies do you use to help you come up with the answer (without using a computer)? Be specific.

I drew out what I thought the code would look like on a piece of paper. To simplify the code I only wrote out what would be shown in the output such as “Java” and “Welcome to”. I could then predict the alignment of the code based on the sign and magnitude of the number such as with 1). This helped a lot with 6) as the \n made the line look cluttered. After writing out the outputs, I could then apply the field numbers and precision that each %f had keeping in mind that I needed to move onto the next line for each output. I know now that I can utilize fancy output to format my system output and some strategies to predicting the results of the program. The use of the %f with the precision will especially be helpful with rounding answers.

Station 5: GUI for user input

INDIVIDUALLY, add at least 3 enhancements to make the program better in some way. Think about how you could improve the program in the areas of input, processing and output.

Original Code

//output title “area of a circle calculator”
//prompt “enter radius of circle”
//get radius
//calculate area=PI(radius)2
//output result “area of the circle is” area

//import Scanner to read user input
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    //create Scanner object to get user input
    Scanner input = new Scanner(System.in);
    //variables
    double area;
    double radius;

    //prompt
    System.out.println("area of a circle calculator");

    //user input
    System.out.println("enter radius of circle");
    radius = input.nextDouble();

    //calculate area
    area = Math.PI * Math.pow(radius, 2);

    //output result
    System.out.println("area of the circle is " + area);   
  }
}

Enhanced Code

//output title “area of a circle calculator”
//prompt “enter radius of circle”
//get radius
//calculate area=PI(radius)2
//output result “area of the circle is” area

//import JOptionPane for fancy input
import javax.swing.JOptionPane;

class Main {
  public static void main(String[] args) {
    //variables
    double area;
    double radius;
    double roundedArea;
    int repeat = 0;

    //explain what program does
    JOptionPane.showMessageDialog(null, "Area of a circle calculator");

    //while loop runs as long as repeat is equal to 0
    while (repeat == 0){    
      //user input
      radius = Double.parseDouble(JOptionPane.showInputDialog("Enter radius of circle"));

      //calculate area
      area = Math.PI * Math.pow(radius, 2);
      roundedArea = Math.round(area *10000.0)/10000.0;

      //output result
      JOptionPane.showMessageDialog(null, "Area of the circle is " + roundedArea);

      //ask if user would like to repeat
      repeat = JOptionPane.showConfirmDialog(null, "Repetition", "Repeat?", JOptionPane.YES_NO_OPTION);
      //0=yes, 1=no -1=x out
    }    
  }
}

Enhancements

The three enhancements that I added were Dialog boxes, a while loop, and a rounded the answer. I believe the dialog boxes are more user friendly as they don’t need to type into the console. The while loop repeated the calculator for as long as the user continued to select yes. This enhancement removes the need to rerun the program after a single calculation. Finally, I rounded the final area to 4 decimal places. The were an unnecessary amount of decimal places in the answer, so I rounded it to what I thought a reasonable amount of decimals would be.

What enhancement would you add to the program if you had an expert level of knowledge about Java?

If I had an expert level knowledge of Java, I’d want to add real time results so that when you type in a radius you get an immediate answer. This would be much more efficient than clicking through the prompts and save lots of time. If that enhancement wasn’t possible. I’d like to add the storage of previous results. I’d store the results that were inputted and display them on the first prompt. Both of these would improve the user experience as the first would require less clicking and the second could provide a user to quickly glance at solutions to previous inputs.