Java basics Part 1 – First calculation and Scanner

In this article we are going to make first calculation and try out scanner. With these tutorials you are going to learn basic use of Java as programing language.

Before anything, theory

Let’s start asking us, now that we know that Eclipse Kepler standard 4,3 works, how can we make it do other thinks than print out text. Well like any other programing language, you should be able to use to do simple calculations and give user ability send feeds.

When talking about calculation you might have constant information and variable information. In Java, if you are going to give any constant information you are going to type something like this: final int PRICE = 35   . And when you type variables you write it : int price = 35  . So what is the difference? The difference is that if you give constant information, program will not try to change it. With variables if you give two times the same variable with two different solutions, that one which is given last is the outcome.

Here is a little list about some key information types that is recommend to use in basic coding.

  • int = means whole numbers (range -2 147 483 648  –  +2 147 483 647)
  • short = means whole numbers (range -32 768  –   +32 767)
  • long = means whole numbers  (range  -2^63   –  +2^63-1)
  • double = means decimal numbers
  • char = unicode-marks
  • boolean = means true or false

Next about user feeds. To get user feeds to Java you need this thing call Scanner. What does it do, it takes user feeds and put them into the Java code. But there is some work to be done here, because you need to type some to get it actually work.

  1. Between package and class text type: import java.util.Scanner; this loads scanner code.
  2. Right after the second { – mark  you need to type this: Scanner input = new Scanner(System.in); this enables scanner to work.
  3. When you add user feeds type something like this: double price = input.nextDouble(); note that input.next____ -you type information type with capital letter.

One thing to remember when using scanner is that you don’t have to repeat phase two. Only repeats in phase three is required when getting multiple feeds.

First Calculation

In this first example I’m going to show how you are able to do simple calculations with Java-language. This code also prints the answer to user. In comment the function of this Java code is told.

package basic;

 /* This program, calculates and prints for user one package of movie tickets
  * that are priced as 35 €. It also tolds how many ticket there are
  * and tells the price of one movie ticket.
  */

public class MovieTicketA {

 public static void main(String[] args) {

 double ticketpackage = 35.00;
 double ticket;

 ticket = (double)ticketpackage / 5;

 System.out.println("One packgae of movie ticket costs 35.0 €. Total sum of 5 tickets"
 +"\nOne ticket will cost then " + ticket + " €");

 }

}

Telling some parts of the code if you don’t quite understand, as to me last one of this list was mystery.

  • when you type variables you don’t have to give them any specific value.
  • why the (double)package , because sometimes the when you try to divade whole numbers, the answer whole number. Here the double makes it possible to have decimal numbers in the code.
  • what means “+ \n ” , this means that there is going to be line break. You can type the text right after it.

Example of scanner

This is one way of using scanner. This example is similar to one above but it’s servers slightly different purpose.

package basic;

import java.util.Scanner;

 /* This program asks from user the price of one package of movie tickets.
 * After this user feed, program counts the price of one movie ticket.
 * One package contains 5 tickets and costs 35,00€
 */

public class MovieTicketB {

 public static void main(String[] args) {

 Scanner input = new Scanner(System.in);

 System.out.print("Hello! Please give price of one movie ticket package: ");
 double ticketpackage = input.nextDouble();
 input.nextLine();
 double ticket;

 ticket = ticketpackage / 5;

 System.out.println("One ticket will cost " + ticket + " €");

 input.close();

 }

}

As you can see it’s not too much text here and the program ask feed from user. Of course in real program it would ask the price and how many tickets it’s in one package. But this was only a brief example of using scanner.

Conclusion

Making these kinds programs with Java-language is good practicing. You might want to do program that serves you a greater purpose. To have this kind of raw power you should easily solve easiest mathematics problems.

– Tuomas Törmä

Sources

  •  Studies in Haaga-Helia Autumn 2013 course Introduction to Programing Teacher Lahtinen Seija
  • Try and err

Leave a comment