Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, September 22, 2016

Processing Delimited String


public class Parse {

    public static void main(String [] args) {
    String arr[];
    String str="E006,Herbert Colanggo,3";
   
    arr=str.split(",");
   
   
   
   
    System.out.println(arr[0]);
    System.out.println(arr[1]);
    }
    
    
}

How To Read A Text File in Java




import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class fileinput {
public static void main(String[] args) throws IOException {
BufferedReader br = null;
String sCurrentLine;

br = new BufferedReader(new FileReader("C:\\Users\\home\\Documents\\Db.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}

}

}

Wednesday, September 21, 2016

How to compute the number of hours between two given times in Java

/**
 * @(#)testcase1.java
 *
 *
 * @author : A. Dichosa
 * @version 1.00 2016/9/21
 */


public class testcase1 {
    public static void main(String [] args) {
    String timein,timeout;
    double time_interval=0; 
        
    timein="8:00";
    timeout="12:00";
    time_interval=GetHourInterval(timein,timeout);
       

    System.out.println(String.format("%.2f",time_interval));    
    }   
    
    /*
     *Description: This method computes the hour interval between two times. It takes two parametes in and out.
     */
    private static double GetHourInterval(String in, String out){
    String timein_arr[];
    String timeout_arr[];
   
    double in_hours=0;
    double out_hours=0;
    double total_hours=0;
   
    timein_arr=in.split(":");
    timeout_arr=out.split(":");
   
    in_hours=Double.parseDouble( timein_arr[0])+ (Double.parseDouble(timein_arr[1])/60);
    out_hours=Double.parseDouble( timeout_arr[0])+ (Double.parseDouble(timeout_arr[1])/60);
    total_hours=out_hours-in_hours;
   
    return total_hours;
   
    }
    
}

Tuesday, September 13, 2016

Java - Files and I/O

Important: Use the comment section below for questions. Use the format {your fullname}: {question}
               
The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. The stream in the java.io package supports many data such as primitives, Object, localized characters, etc.

Stream

InPutStream: The InputStream is used to read data from a source.
OutPutStream: the OutputStream is used for writing data to a destination.

A stream can be defined as a sequence of data. there are two kinds of Streams




Java I/O Streams



Java provides strong but flexible support for I/O related to Files and networks but this tutorial covers very basic functionality related to streams and I/O. We would see most commonly used example one by one:

Byte Streams

Java byte streams are used to perform input and output of 8-bit bytes (for reference check the table of 8-bit characters). Though there are many classes related to byte streams but the most frequently used classes are , FileInputStream and FileOutputStream. Following is an example which makes use of these two classes to copy an input file into an output file:

import java.io.*;

public class CopyFile {
   public static void main(String args[]) throws IOException
   {
      FileInputStream in = null;
      FileOutputStream out = null;

      try {
         in = new FileInputStream("input.txt");
         out = new FileOutputStream("output.txt");
         
         int c;
         while ((c = in.read()) != -1) {
            out.write(c);
         }
      }finally {
         if (in != null) {
            in.close();
         }
         if (out != null) {
            out.close();
         }
      }
   }
}
Now let's have a file input.txt with the following content:
This is test for copy file.
As a next step, compile above program and execute it, which will result in creating output.txt file with the same content as we have in input.txt. 

Dissecting the Program
What the program does is just to copy the content of file input.txt and write it down to output.txt.

CHECK YOURSELF
What would be written to file output.txt if we run these statements individually?

  1. out.write(078);
  2. out.write(079);
  3. out.write(095);
  4. out.write(080);
  5. out.write(065);
  6. out.write(073);
  7. out.write(078);
  8. out.write(095);
  9. out.write(078);
  10. out.write(079);
  11. out.write(095);
  12. out.write(071);
  13. out.write(065);
  14. out.write(073);
  15. out.write(078);



Tuesday, July 12, 2016

Solus and Gnome - COMPRO6

Lab Activity #3:

To those who did not make it in time last session's activity, you need to comply by submitting the attached source code to email adichosa@gmail.com with email subject "Act3-Late-[Your Section]-[Fullname]". This should be sent to my email not later than 12PM for Solus and 3PM for Gnome.

-------------------------------------------------

Lab Activity #4:

Problem:

Write a program to accept five integers from the keyboard and store them to array arr. Process the array and display the sum, average, min and max.

Sample Output:

Enter element 1: 3
Enter element 2: 6
Enter element 3: 2
Enter element 4: 10
Enter element 5: 31

Processing...

Sum: 56
Average: 11.2
Min: 2
Max: 31

Important: Email to adichosa@gmail.com with subject "Act4-[Your Section]-[Your name]".

Very very very ... very IMPORTANT:
I am appointing Mr. Togle of Solus and Mr. Pedrajas of Gnome to jot-down the names of those who are making loud noises during the session. Necessary deductions to Activity #4 will be imposed to those who are in the list.

Tuesday, June 28, 2016

Input Stream

Activity #2


Problem 1:

Write a program to require the user to input a string. Process the string by counting the total number of vowels and consonants in the string. For additional reference, please visit http://www.tutorialspoint.com/java/java_strings.htm , http://bit.ly/28TBVbB


VALIDATION

Sample Output #1:
Enter a string: Chance favors the prepared minds.

Vowels: 9
Consonants: 19

Sample Output #2:
Enter a string: The quieter you become, the more you are able to hear!

Vowels: 22
Consonants: 20

Sample Output #3:
Enter a string: Swing your rope down low.

Vowels: 7
Consonants: 13

------------------------------------------------------------------------------------------------------
Problem 2:

Write a program to require a string input from the keyboard. Using a loop structure, read the string backwards and display the result.

Sample Output #1:
Enter a string: Government
tnemnrevoG


Sample Output #2:
Enter a string: Aliens
sneilA

Sample Output #3:
Enter a string: Absurd
drusbA





Note: Once you're done, email the source code to adichosa@gmail.com. Use as subject <SECTION>-Act2-<fullname>. Deadline is on or before 12PM for Solus and 3PM for GNOME. TODAY!!!

Sunday, May 22, 2016

FIND THE SECOND LARGEST NUMBER IN THE ARRAY

/**
* find the second largest integer in an array.
* @param ia an array of integers
* @return the second largest number in ia
*/
private static int secondLargest(int[] ia)
{
int first_largest=0;
int second_largest=0;

//find the first largest
for (int i=0; i<ia.length; i++){
if (ia[i] > first_largest)//determine first the first largest so that we can identify the second largest
first_largest=ia[i];

}
//find the second largest
for (int i=0; i<ia.length; i++){
if (ia[i] > second_largest && ia[i]<first_largest) //check if it is the second largest
second_largest=ia[i];

}

return second_largest;
} //secondLargest

Monday, September 1, 2014

Prime Numbers with for Loop

public class GeneratePrimeNo {
        
        public static void main(String[] args) {
   
            int max = 100;
   
            System.out.println("Generate Prime numbers between 1 and " + max);
   
            // loop through the numbers one by one
            for (int i = 1; i<max; i++) {
   
                boolean isPrimeNumber = true;
   
                // check to see if the number is prime
                for (int j = 2; j < i; j++) {
                    if (i % j == 0) {
                        isPrimeNumber = false;
                        break; // exit the inner for loop
                    }
                }
                
                // print the number if prime
                if (isPrimeNumber) {
                  System.out.print(i + " ");
                }
            }
   
        }
   
    }

Friday, August 29, 2014

Program Tracing #1

What is the output of this program?

class Increment
{
public static void main(String arg[])
{
int g = 3;
System.out.print(++g * 8);
}
}


a) 25
b) 24
c) 32
d) 33

What is a Pseudocode?

"Pseudocode is an artificial and informal language that helps programmers develop algorithms."

Pseudocode programs are not actually executed on computers. Rather, they help the programmer "think out" a program before attempting to write it in a programming language, such as Java or C++. Pseudocode normally describes only executable statements -- the actions that are performed when the program is converted from pseudocode to any programming language and is run. Declarations are not executable statements. For example, the declaration

            int i;

tells the compiler the type of variable i and instructs the compiler to reserve space in memory for the variable. This declaration does not cause any action -- such as input, output or a calculation -- to occur when the program is executed. Some programmers choose to list variables and mention the prpose of each at the beginning of a pseudocode program.

For Viral Stuff and Trending News please visit www.fooviral.com