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

Contact Us at blog.kerul.net

Powered by EMF HTML Contact Form

Bootstrap Template for PHP database system - MyCompanyHR

HTML without framework is dull. Doing hard-coded CSS and JS are quite difficult with no promising result on cross platform compatibility. So I decided to explore BootStrap as they said it is the most popular web framework. What is BootStrap? - Bootstrap is the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first web sites. (  http://www.w3schools.com/bootstrap/   ) Available here -  http://getbootstrap.com/ Why you need Flat-UI? Seems like a beautiful theme to make my site look professional. Anyway you could get variety of BootStrap theme out there, feel free to select here  http://bootstraphero.com/the-big-badass-list-of-twitter-bootstrap-resources/ Flat-UI is from DesignModo -   http://designmodo.com/flat/ Web Programming MyCompanyHR – PHP & MySQL mini project (with Boostrap HTML framework) Template 1: Template for the Lab Exercise. This is a project sample of a staff record management system. It has the PHP structured co

The Challenges of Handling Proverbs in Malay-English Machine Translation – a research paper

*This paper was presented in the 14th International Conference on Translation 2013 ( http://ppa14atf.usm.my/ ). 27 – 29 August 2013, Universiti Sains Malaysia, Penang. The PDF version is here: www.scribd.com/doc/163669571/Khirulnizam-The-Challenges-of-Automated-Detection-and-Translation-of-Malay-Proverb The test data is here: http://www.scribd.com/doc/163669588/Test-Data-for-the-Research-Paper-the-Challenges-of-Handling-Proverbs-in-Malay-English-Machine-Translation Khirulnizam Abd Rahman, Faculty of Information Science & Technology, KUIS Abstract: Proverb is a unique feature of Malay language in which a message or advice is not communicated indirectly through metaphoric phrases. However, the use of proverb will cause confusion or misinterpretation if one does not familiar with the phrases since they cannot be translated literally. This paper will discuss the process of automated filtering of Malay proverb in Malay text. The next process is translation. In machine translatio

Most used STRING functions in my PHP coding

These are my favourite string manipulation functions in my daily coding life. Dedicated especially to Web Programming students. Read them and have fun. Expect a happiness after a storm , and you’ll find your “inner peace”… This post is still in draft. I’ll update and refine with more examples that I’ve personally develop. More after the break…