Skip to main content

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.




Comments

Popular posts from this blog

Several English proverbs and the Malay pair

Or you could download here for the Malay proverbs app – https://play.google.com/store/apps/details?id=net.kerul.peribahasa English proverbs and the Malay pair Corpus Reference: Amir Muslim, 2009. Peribahasa dan ungkapan Inggeris-Melayu. DBP, Kuala Lumpur http://books.google.com.my/books/about/Peribahasa_dan_ungkapan_Inggeris_Melayu.html?id=bgwwQwAACAAJ CTRL+F to search Proverbs in English Definition in English Similar Malay Proverbs Definition in Malay 1 Where there is a country, there are people. A country must have people. Ada air adalah ikan. Ada negeri adalah rakyatnya. 2 Dry bread at home is better than roast meat home's the best hujan emas di negeri orang,hujan batu di negeri sendiri Betapa baik pun tempat orang, baik lagi tempat sendiri. 3 There's no accounting for tastes We can't assume that every people have a same feel Kepala sama hitam hati lain-lain. Dalam kehidupan ini, setiap insan berbeza cara, kesukaan, perangai, tabia

Submit your blog address here

Create your own blog and send the address by submitting the comment of this article. Make sure to provide your full name, matrix and URL address of your blog. Refer to the picture below. Manual on developing a blog using blogger.com and AdSense, download here … Download Windows Live Writer (a superb offline blog post editor)

Applications of Web 2.0

Web 2.0 describes the changing trends in the use of World Wide Web technology and web design that aim to enhance creativity , secure information sharing, collaboration and functionality of the web. Web 2.0 concepts have led to the development and evolution of web-based communities and hosted services , such as social-networking sites , video sharing sites , wikis , blogs . Find a website or web application that conform to the criteria of Web 2.0. Put the name of the application and the URL in the comment below. Please provide your full name and matrix number. Make sure the application you choose is not already chosen by your friend in the previous comment.

ViewFlipper Example–a simple FlashCard

UPDATE: Improved with Fling gesture (Sept 2012) UPDATE: ViewFlipper with Flip-In and Flip-Out Animation (August 2012) This tutorial is to demonstrate the ViewFlipper layout that is almost similar to CardLayout (in Java). The app will produce a simple Flash card that provide several screens with different picture for each card. Flip-in and Flip-out animation provided. Added in Sept 2012 – an improvement to support Fling gesture – enjoy… The amendment is only on the coding part. Some how the layout design (main.xml) is quite long. Later I’ll produce separated screen by including several XML layout from outside files. Screenshots;

ASK kerul pls...

Ask me please, just click the button... Any question related to the web, PHP, database, internet, this blog, my papers, or anything. I'll answer to you as soon as I can, if related to my domain. If not I'll try to find out from Google and provide you a link that can help you solve a problem. Sincerely, kerul. ASK kerul pls... http://kerul.blogspot.com