Skip to content

Instantly share code, notes, and snippets.

@jimmy087
jimmy087 / Exercise1.java
Created May 4, 2013 05:42
Simple program which calculates tax and net pay
public class Exercise1 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter employee name: ");
String employeeName = input.nextLine();
System.out.println("Number of hours worked: ");
int numberOfHoursWorked = input.nextInt();
System.out.println("Hourly wage rate: ");
double wageRate = input.nextDouble();
System.out.println("Enter the Federal tax rate: ");
@jimmy087
jimmy087 / Calculator.java
Created May 4, 2013 05:45
A program for command line calculator.
// A program for command line calculator.
public class Calculator {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Incorrect input... there must be single space between operands.");
System.exit(1);
}
// The result of the operation.
@jimmy087
jimmy087 / ChechPalindrome.java
Created May 4, 2013 05:46
Check whether a string is a palindrome
public class CheckPalindrome {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter the string: " );
String s = input.nextLine();
if (isPalindrome(s)) {
System.out.print( s + " is a palindrome.");
}
else {
System.out.print(s + " is not a palindrome.");
@jimmy087
jimmy087 / CountEachLetter.java
Created May 4, 2013 05:48
Count each letter in the string
public class CountEachLetter {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter the string: ");
String s = input.nextLine();
// Invoke the countLetters method to count each letter
int[] counts = countLetters(s.toLowerCase());
//Display results
@jimmy087
jimmy087 / Exercise9_1.java
Created May 4, 2013 05:50
Check whether SSN is correct or not.
import java.util.Scanner;
public class Exercise9_1 {
public static void main(String[] args) {
System.out.println("Please enter your SSN: ");
Scanner input = new Scanner(System.in);
String s = input.nextLine();
if (s.matches("\\d{3}-\\d{2}-\\d{4}")) {
System.out.println("The SSN entered by you is correct.");
}
else {
@jimmy087
jimmy087 / Exercise9_2.java
Created May 4, 2013 05:51
Check whether a string is a sub string of another string.
import java.util.Scanner;
public class Exercise9_2 {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("Please enter the first string: ");
String s1 = input.nextLine();
System.out.println("Please enter the second string: ");
String s2 = input.nextLine();
if (s1.indexOf(s2) != -1) {
@jimmy087
jimmy087 / Exercise9_3.java
Created May 4, 2013 05:53
A password is valid or not
import java.util.Scanner;
public class Exercise9_3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the password: ");
String password = input.nextLine();
if (isValid(password)) {
System.out.println("The password is valid.");
}
@jimmy087
jimmy087 / Exercise9_4.java
Created May 4, 2013 05:56
checks how many times a character appears in a string.
import java.util.Scanner;
public class Exercise9_4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a string: ");
String s1 = input.nextLine();
System.out.println("Please enter a character: ");
char s2 = input.nextLine().charAt(0);
int counts = counts(s1, s2);
@jimmy087
jimmy087 / Exercise9_5.java
Created May 4, 2013 05:59
Count number of appearance of a digit in a string.
public class Exercise9_5 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter a string: ");
String s1 = input.nextLine();
int [] counts = countDigits(s1);
// Display results.
for (int i = 0; i < counts.length; i++) {
if (counts [i] > 0) {
@jimmy087
jimmy087 / Exercise9_7.java
Created May 4, 2013 06:01
A basic phone keypad
// A phone keypad
public class Exercise9_7 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter the string: ");
String s1 = input.nextLine();
if (isValid(s1)) {
System.out.println("The number is valid.");
String s2 = s1.toLowerCase();