Skip to content

Instantly share code, notes, and snippets.

View edolopez's full-sized avatar
🎯
Focusing

Eduardo Lopez De Leon edolopez

🎯
Focusing
View GitHub Profile
@edolopez
edolopez / Strings.java
Created April 9, 2014 13:58
Try-Catch block in Java, a simple example
import java.util.*;
import java.io.*;
public class Strings {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
try {
input.nextInt();
System.out.println("Thanks");
} catch (InputMismatchException e) {
@edolopez
edolopez / NumbersInFile.java
Last active March 31, 2025 10:01
Example of writing and reading Text Files in Java
import java.io.*;
import java.util.*;
public class NumbersInFile {
public static void main(String[] args) throws IOException {
//
// READING FROM A FILE
//
try {
@edolopez
edolopez / ReadFile
Created April 7, 2014 13:29
An example of Reading a File with the Scanner Class
import java.io.*;
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
Scanner file = new Scanner(new File("info.txt"));
int numLines = file.nextInt();
@edolopez
edolopez / Circle.java
Created March 24, 2014 14:34
Graphical Representation for OOP
// Reference for this Class:
// http://www.ntu.edu.sg/home/ehchua/programming/java/images/ExerciseOOP_Circle.png
//
public class Circle {
private double radius;
private String color;
public Circle() {
@edolopez
edolopez / Puppy.java
Created March 23, 2014 17:00
Puppy class and PuppyTest (with the main method), an example of Objects
public class Puppy {
// Instance Variables
int age;
String color;
String breed;
double weight;
String name;
public Puppy() {
name = "Default";
@edolopez
edolopez / Binary.java
Created March 2, 2014 22:34
Binary Search method in Java
public static int binarySearch(int numbers[], int key) {
// Select highest and lowest indexes on the array
int mid;
int low = 0;
int high = numbers.length - 1;
// Continue searching while low is still smaller than high
while (low <= high) {
mid = (low + high) / 2; // Binary partition
if (numbers[mid] > key) {
@edolopez
edolopez / Sequential.java
Created March 2, 2014 21:53
Sequential search method in Java
public static int search(int[] numbers, int key) {
for (int index = 0; index < numbers.length; index++) {
if (numbers[index] == key)
return index; // Founds the elemnt and finishes the search
}
// If we get to the end of the loop, a value has not yet
// been returned. We did not find the key in this array.
return -1;
}
@edolopez
edolopez / Partial1CSII.java
Created February 18, 2014 14:54
This is a possible way to answer the methods for the Partial 1 in CS II class.
public class Partial1CSII {
public static void main(String[] args) {
// Calling exercise II
System.out.println(lowest(4,3,-45));
// Calling exercise III
int[] numbers = new int[]{4, 20, 35, 4, 6};
System.out.println(diff(numbers));
@edolopez
edolopez / ArraysBasics.java
Last active August 29, 2015 13:56
Examples using Arrays in Java. This is the first assignment for CS II.
import java.util.Scanner;
public class ArraysBasics {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int meanAge = 0, i;
// Initialization of the two arrays depending on user input
int positions = input.nextInt();
int[] myArray = new int[positions];
@edolopez
edolopez / Fibonacci.java
Created January 24, 2014 13:15
Fibonacci with method in Java
import java.util.Scanner;
public class Fibonacci {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
// Asks for the first number
int num = input.nextInt();