Friday, March 25, 2011

Learning Java 03 - Scanner

Scanner is use to get input from the user and then this data will be store inside a variables. Then you can display this data on screen.

The program below will ask an input from the user then it will display what the user entered.


import java.util.*;

public class java03{
    public static void main(String[]args){
       
        Scanner scan = new Scanner(System.in);
       
        System.out.print("Enter your age: ");
        int age = scan.nextInt();
       
        System.out.println("\nYour age is "+ age);
    }
}


output:
// the user must input integer here, example 20.
Enter your age: 20
// then it will display what the user entered.
Your age is 20


Scanner scan = new Scanner(System.in); - Command for the declaration of Scanner, in this statement the name the Scanner is "scan" Note: You can change the name of your scanner as long as its not started in integers.
example:
  • Scanner input1 = new Scanner(System.in);
  • Scanner input2 = new Scanner(System.in);
int age = scan.nextInt(); - The command that will ask the user to input a data. Then this data will be stored in the variable.
import java.util.*; - This is the symbol for util package. If you will declare a command statement for Scanner, you must encode this symbol first at beginning of your code. Because this symbol holds the Scanner.

Wednesday, March 23, 2011

Learning Java 02 - Variables

Variables are data storage. The most common variable in java is "int", it stores number. The program below will display "Your age is 15". Because the value of "age" is 15. So if you will change its value, the output will also change.


public class variable{
    public static void main(String[]args){
      
        int age = 15;
        System.out.println("Your age is "+ age);
    }
}


int - the declaration of variable integer, so it must only content numbers. You can also use "double" for decimal numbers.
age - it is the name of your integer. Note: you can change its label as long as it's not started with numbers.

example:
  • int age1 = 15;
  • int age2 = 20;
  • int num1 = 43;

Monday, March 21, 2011

Learning Java 01 - Hello World

This is a simple java program. This program will just display "Hello World" then exit. 

public class java01
{
       public static void main(String[]args)
       {

       System.out.println("Hello World");
       }
}


public class - this function is the declaration of the filename, so in this program java01 is the filename.
System.out.println - its the command to display, so anything you'll encode inside in it will display on screen when you run the program. You can also use "System.out.print" if you don't want to break one line after displaying what you wanted to display.
public static void main(String[]args) - click here for more info about this.

Sunday, March 20, 2011

Learning Java 00

You can use any operating system in programming a simple java language. If I'm in windows, I use "JCreator" in typing my codes, compiling my program and running it. But if I'm in Linux I'm using the terminal.

So if you're gonna use windows in programming a java language, use "JCreator". Just download and install it, then you can start. You can also use netbeans or eclipse. But I'm recommending you to use JCreator cause it's easy to use. If you need help in finding JCreator on internet just comment on this post.

If you are going to use Linux operating system, use the "text editor" in typing your codes. Then compile and run your program in terminal. But you had install first "sun java" on your system before get started.

Enter this command in terminal one by one to install java:
  • sudo add-apt-repository ppa:sun-java-community-team/sun-java6
  • sudo apt-get update
  • sudo apt-get install sun-java6

In compiling and running the program in terminal, the first thing to do is to search the directory in terminal where your file located. Then compile and run it.

javac filename.java - use this command in compiling your program. Note: filename is the name of your file, so it's not static.
java filename - then use this command in running your program. Note: if you made changes on your program, compile it again before running it so the changes you made takes effect.

Friday, March 18, 2011

Matrix Operation in Java

Last time, I struggle on how to repeat the whole program. How to run it again if the users want to run it again and exits if want to exit. Now I've found one solution how to solve it but I think there's a lot of solution out there to fixed that problem. And what I did is put the whole program in the "while loop" then condition the increment. That's all!

int x=0;
while(x>=0){//the whole program is here!}
x++;//condition the increment..


Okay..here's what I did. This is the perfect program what our instructor been looking for. But it's too late cause we've already got a grade of 65..lol!

import java.util.*;
public class descretematrix{
    public static void main(String[]args){
        
        System.out.println("\nMATRIX");
        int priloop=0;
        while(priloop>=0){
            int secloop=0;
            while(secloop>=0){            
        Scanner scan=new Scanner(System.in);
            System.out.println("\nMenu:");
            System.out.println("1=Addition!");
            System.out.println("2=Subtraction!");
            System.out.println("3=Multiplication!");
            System.out.println("0=Exit!");
            System.out.print("\nSelect a choice: ");
            int choice = scan.nextInt();
            if(choice==1){ System.out.print("\nADDITION OF MATRIX:");
                    System.out.print("\nEnter row of first table: ");
                    int row1=scan.nextInt();
                    System.out.print("Enter column of first table: ");
                    int col1=scan.nextInt();
                    System.out.print("Enter row of second table: ");
                    int row2=scan.nextInt();
                    System.out.print("Enter column of second table: ");
                    int col2=scan.nextInt();
                    
                    if(row1==row2 && col1==col2){
                    int mat1[][]=new int[100][100];
                    int mat2[][]=new int[100][100];
                System.out.println();
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of first table = ");
                    mat1[i][j]=scan.nextInt();}}
                    
                    System.out.println();
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of second table = ");
                    mat2[i][j]=scan.nextInt();}}                    
                    
                    System.out.println("\nTable 1");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print(mat1[i][j]+" ");
                    }System.out.println();}
                    
                    System.out.println("\nTable 2");                    
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print(mat2[i][j]+" ");
                    }System.out.println();}
                                        
                    System.out.println("\nAdded Matrix");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print((mat1[i][j]+mat2[i][j])+" ");
                    }System.out.println();}
                    }                    
                    else{
                    System.out.println("\nSorry! The Matrix cannot be Add!");
                    System.out.println("Please enter the same size of table!");
                    }break;}
                    
            else if(choice==2){ System.out.print("\nSUBTRACTION OF MATRIX:");
                    System.out.print("\nEnter row of first table: ");
                    int row1=scan.nextInt();
                    System.out.print("Enter column of first table: ");
                    int col1=scan.nextInt();
                    System.out.print("Enter row of second table: ");
                    int row2=scan.nextInt();
                    System.out.print("Enter column of second table: ");
                    int col2=scan.nextInt();
                    
                    if(row1==row2 && col1==col2){
                    int mat1[][]=new int[100][100];
                    int mat2[][]=new int[100][100];
                System.out.println();
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of first table = ");
                    mat1[i][j]=scan.nextInt();}}
                    
                    System.out.println();
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of second table = ");
                    mat2[i][j]=scan.nextInt();}}                    
                    
                    System.out.println("\nTable 1");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print(mat1[i][j]+" ");
                    }System.out.println();}
                    
                    System.out.println("\nTable 2");                    
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print(mat2[i][j]+" ");
                    }System.out.println();}
                                        
                    System.out.println("\nSubtracted Matrix");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print((mat1[i][j]-mat2[i][j])+" ");
                    }System.out.println();}
                    }                    
                    else{
                    System.out.println("\nSorry! The Matrix cannot be Subtract!");
                    System.out.println("Please enter the same size of table!");
                    }break;}                        
            
            else if(choice==3){ System.out.print("\nMULTIPLICATION OF MATRIX:");
                    System.out.print("\nEnter row of first table: ");
                    int row1=scan.nextInt();
                    System.out.print("Enter column of first table: ");
                    int col1=scan.nextInt();
                    System.out.print("Enter row of second table: ");
                    int row2=scan.nextInt();
                    System.out.print("Enter column of second table: ");
                    int col2=scan.nextInt();
                    
                    if(row1==row2 && col1==col2){
                    int mat1[][]=new int[100][100];
                    int mat2[][]=new int[100][100];
                    int mat3[][]=new int[100][100];                    
                System.out.println();
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of first table = ");
                    mat1[i][j]=scan.nextInt();}}
                    
                    System.out.println();
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of second table = ");
                    mat2[i][j]=scan.nextInt();}}                    
                    
                    System.out.println("\nTable 1");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print(mat1[i][j]+" ");
                    }System.out.println();}
                    
                    System.out.println("\nTable 2");                    
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print(mat2[i][j]+" ");
                    }System.out.println();}                    
                    
                    for(int i=0;i<=row1;i++){    
                    for(int j=0;j<=col1;j++){
                    for(int k=0;k<=row1;k++){
                    mat3[i][j] += mat1[i][k] * mat2[k][j];}}}                    
                                                    
                    System.out.println("\nMultiplied Matrix");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print(mat3[i][j]+" ");}
                    System.out.println();}
                    }        
                        
                    else if(row1==col2){
                    int mat1[][]=new int[100][100];
                    int mat2[][]=new int[100][100];
                    int mat3[][]=new int[100][100];                    
                System.out.println();
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of first table = ");
                    mat1[i][j]=scan.nextInt();}}
                    
                    System.out.println();
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of second table = ");
                    mat2[i][j]=scan.nextInt();}}                    
                    
                    System.out.println("\nTable 1");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print(mat1[i][j]+" ");
                    }System.out.println();}
                    
                    System.out.println("\nTable 2");                    
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print(mat2[i][j]+" ");
                    }System.out.println();}                    
                    
                    for(int i=0;i<row1;i++){    
                    for(int j=0;j<col1;j++){
                    for(int k=0;k<=row1;k++){
                    mat3[i][j] += mat1[i][k] * mat2[k][j];}}}                    
                                                    
                    System.out.println("\nMultiplied Matrix");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print(mat3[i][j]+" ");}
                    System.out.println();}
                    }                                
                    else{
                    System.out.println("\nSorry! The Matrix cannot be Multiply!");
                    System.out.println("Please enter the same size of table!");
                    System.out.println("Or enter same size of row 1 and column 2!");
                    }break;}
                                                
            else if(choice==0){System.exit(0);}
            else{
                System.out.println("\nPlease Select from 1-4 only!\n");
                secloop++;}            
            }    
            
            int triloop=0;
            while(triloop>=0){
        Scanner scaned=new Scanner(System.in);
            System.out.println("\nStart Again?");
            System.out.println("1=start again!");
            System.out.println("0=exit!");
            System.out.print("\nSelect a Choice: ");
            int choice2=scaned.nextInt();
            if(choice2==1){break;}
            else if(choice2==0){System.exit(0);}
            else {
                System.out.print("\nPlease Select 1 and 0 only!");
                triloop++;}}        
        priloop++;}        
    }
}

Tuesday, March 8, 2011

Matrix Addition, Subtraction and Multiplication in Java

This a simple java code which add, subtract and multiply matrices. In running this program just enter the row and column size, and then you will input the element of the first and second matrix based on the row and column size you created. Then it will print out the added, subtracted and multiplied matrix.

My problem in this program is how to repeat it all over again. You can see at the last part of this program I made this statement "Play again? [Y/N]". If the user input "y" or "Y", it should run the program again otherwise the program will exit. And my problem is it won't run again if i select "y" or "Y", but there's no problem in compiling.

If anybody here can help me..show me some codes please..thanks.


import java.io.*;
import java.util.*;
public class matrixadd{
   
public static void main(String[]args)throws IOException{
   
    Scanner scan = new Scanner(System.in);
   
    int mat1[][]=new int [10][10];
    int mat2[][]=new int [10][10];
    int mat3[][]=new int [10][10];
   
    System.out.print("Number of row: ");
    int row = scan.nextInt();
    System.out.print("Number of column: ");
    int col = scan.nextInt();
    System.out.println();
   
    for(int i=0; i<row; i++){
    for(int j=0; j<col; j++){
        System.out.print("Enter element "+(i+1)+":"+(j+1)+" of first table = ");
        mat1[i][j] = scan.nextInt();
        } }
   
    System.out.println("\nTable 1:");
    for(int i=0; i<row; i++){
    for(int j=0; j<col; j++){
        System.out.print(mat1[i][j]+" ");
    }System.out.println();
    }
   
    System.out.println();
    for(int i=0; i<row; i++){
    for(int j=0; j<col; j++){
        System.out.print("Enter element "+(i+1)+":"+(j+1)+" of second table = ");
        mat2[i][j] = scan.nextInt();
    }}
   
    System.out.println("\nTable 2:");
    for(int i=0; i<row; i++){
    for(int j=0; j<col; j++){
        System.out.print(mat2[i][j]+" ");
    }System.out.println();
    }
   
    System.out.println("\nAdded Matrix: ");
    for(int i=0; i<row; i++){
    for(int j=0; j<col; j++){
        mat3[i][j] = mat1[i][j] + mat2[i][j];
        System.out.print(mat3[i][j]+" ");
    }System.out.println();
    }
   
        System.out.println("\nSubtracted Matrix: ");
    for(int i=0; i<row; i++){
    for(int j=0; j<col; j++){
        mat3[i][j] = mat1[i][j] - mat2[i][j];
        System.out.print(mat3[i][j]+" ");
    }System.out.println();
    }
   
    int mat4[][]=new int[10][10];
    for(int i=0;i<row;i++){
    for(int j=0;j<col;j++){
    for(int k=0;k<col;k++){
        mat4[i][j]+=mat1[i][k]*mat2[k][j];
    }}}

    System.out.println("\nMultiplied Matrix:");
    for(int i=0;i<row;i++){
    for(int j=0;j<col;j++){
        System.out.print(mat4[i][j]+" ");
    }System.out.println();
    }

BufferedReader geoff = new BufferedReader(new InputStreamReader(System.in));
   
System.out.println("\nPlay again?[Y/N]\n");
String answer = geoff.readLine();
if(answer == "Y" || answer == "y"){
System matrixadd;
} else {
System.exit(0);
} }}