Java Beginner Tutorials

DTCP2023 - FUNDAMENTALS OF PROGRAMMING

Slide Chapter 1 Complex Control Structure


Slide Chapter 2 Java Method


Example Method 1 – Min, Max, Average of 3 numbers

//********************************************************************
//  Demonstrates the use of methods in Java.
//********************************************************************

import java.util.Scanner;

public class MinOfThree
{
   //-----------------------------------------------------------------
   //  Reads three integers from the user and determines the smallest
   //  value.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      int num1, num2, num3, min = 0, max=0;
      float average;

      Scanner scan = new Scanner (System.in);

      displayinfo();

      System.out.println ("Enter three integers: ");
      num1 = scan.nextInt();
      num2 = scan.nextInt();
      num3 = scan.nextInt();

      //min=minimum(num1, num2, num3);
      System.out.println ("Minimum value: " + minimum(num1, num2, num3));

      //call maximum method
      max=maximum(num1, num2, num3);
      System.out.println ("Maximum value: " + max);

      //call average method
      findaverage(num1, num2, num3);

   }//end main

   public void findaverage(int num1, int num2, int num3){
      float average=(num1+num2+num3)/3;
      System.out.println ("Average value: " + average);
   }//findaverage

   public static void  displayinfo(){
      System.out.println ("************************************");
      System.out.println ("A Program to find min and max number");
      System.out.println ("Author: Kerul.net");
      System.out.println ("************************************");
   }//end displayinfo

   public static int minimum(int num1, int num2, int num3){
         int min;
         if (num1 < num2){
         if (num1 < num3)
            min = num1;
         else
            min = num3;
         }
      else{
         if (num2 < num3)
            min = num2;
         else
            min = num3;
      }
      return min;

   }//end minimum

   public static int maximum(int num1, int num2, int num3){
         int max;
         if (num1 > num2)
         if (num1 > num3)
            max = num1;
         else
            max = num3;
      else
         if (num2 > num3)
            max = num2;
         else
            max = num3;
      return max;
   }

}//end class


Example Method 2 – Grade Method



import java.util.Scanner;

public class grademethod
{
   public static void main (String[] args)
   {
      Scanner scan = new Scanner (System.in);
      System.out.println ("Enter your score: ");
      float score = scan.nextInt();
      //call printGrade here

   }//end main

   //Call the printGrade method from main
    public static void printGrade(double score) {
        if (score >= 90.0)
          System.out.println("A");
        else if (score >= 80.0)
          System.out.println("B");
        else if (score >= 70.0)
          System.out.println("C");
        else if (score >= 60.0)
          System.out.println("D");
        else
          System.out.println("F");
  }//end printGrade
}//end class


Slide Chapter 3 Arrays in Java


Sample 1:


public class AverageArray {

    public static void main(String[] args) {
        //int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;
        //int[] num = new int[10];
        int num[]={23,34,13,12,32,10,11,31,45,67};
        /*num[0]=23;
        num[1]=34;
        num[2]=13;
        num[3]=12;
        num[4]=32;*/
        float total=0,average;
        //float average=(num1+num2+num3+num4+num5+num6+num7+num8+num9+num10)10;
        for (int i=0; i<10;i++){
            total=total+num[i];
        }

        average=total/10;
        System.out.println("Total="+total);
        System.out.println("Average="+average);

        //Exercise: List all values that bigger than the average
        for (int i=0; i<10;i++){
            if (num[i]>average){
                System.out.println(num[i]+" is bigger than average");
            }
        }

    }//end main

}

Exercise 1: Get 15 numbers from user, and store the values inside an array variable. Evaluate the biggest and smallest number among the values.


Slide Chapter 4 Class in Java

Slide Chapter 5 File IO in Java



Example: Class and Method

import java.io.*;

//the file name is AverageArray.java
public class MidsemAverageArray {

    private static double[] num = new double[10];
    private static double total_val=0, average_val;
    private static DataInputStream io=new DataInputStream(System.in);

    //method to receive 10 numbers from the user2
    public static void input10num() throws IOException{
        for(int i=0; i<10; i++){
            System.out.println("Input number "+(i+1));
            num[i]=Double.parseDouble(io.readLine());
        }
    }

    //method to calculate the sum of numbers
    public static void calculate_total(){
        //use for loop to receive sum the 10 numbers from the array
        //store the sum in total_val
        for(int i=0; i<10; i++){
            total_val=total_val+num[i];
        }
    }

    //method to calculate the average of numbers
    public static void calculate_average(){
        //calculate the average of the 10 numbers
        //store the sum in average_val
        average_val=total_val/10;
    }

    //main method
    public static void main(String[] args) throws IOException {
        //display the program name and what it does
        //call method input10num()
        input10num();
        //call method calculate_total()
        calculate_total();
        //display the sum of the 10 numbers
        System.out.println("Sum of 10 numbers= "+total_val);
        //call method calculate_average()
        calculate_average();
        //display the average of the 10 numbers
        System.out.println("Average of 10 numbers= "+average_val);

        //initiate class MinMax and declare mm object
        MinMax mm = new MinMax();
        //min
        double min_val = mm.min(num);
        System.out.println("Min among 10 numbers= "+min_val);
        //max
        double max_val = mm.max(num);

    }
}

class MinMax {

    public MinMax() {
    }

    public double min(double num[]){
        double min_process=num[0];
        for(int i=0; i<10; i++){
            if (num[i]<min_process){
                min_process=num[i];
            }
        }
        return min_process;
    }

    public double max(double num[]){

        return 0;
    }


}
 

Found this website and has a easy, simple Java Tutorial, thanks to the author.




Install Java The first step to programming Java is to get it running on your computer. This easy-to-follow guide will show you how to set up Java and Eclipse, complete with screenshots to help you follow along.

Your First Program Got Java and Eclipse installed? Let's make sure everything is set up properly and begin your introduction to Java by starting you with coding your very first program!

Java Output Learn how to easily get Java to display text on the screen. It'll be your proof that you can make a computer do what YOU want it to do!

Java Variables Without variables, there's no way to store values in your program. Learn the basics about creating and using Java's variables so your programs can do more than just display stuff on the screen.

Java Input Want to make your programs interactive? Learning to get input from a user is crucial if your program will ever be used by a human. Java has an easy way of doing this, and that's by using a Java Scanner.

Conditionals ( The If Statement ) Your program is doomed to do exact same thing every time it runs unless you let it know about different conditions. Learn the power of if statements and how to use them to produce different results based on what you tell the code to do.

Loops - For Loops And While Loops If you've been trying out your own programs, you must be getting fed up with having to copy and paste code you want done over and over again. But, there is a better way! Read on to learn how you can make a program do something over and over without having to write it over and over.

Intro To Java Methods The final tutorial in Java For Beginners! Learn a little bit about what Java methods are, how to write them, and how to use them. This tutorial won't cover everything, but it'll be just enough to get you to use them and serves as a jump-off point to the more advanced tutorials.

The tools

1. JDK - download here.
2. Eclipse IDE - download here.




Popular Posts

WorldConferences.net