Category Archives: Programming

Java Programming: Basic 2D Array

/*
Name: Kevin Whetstone
Date: 11/15/13
Program: Basic 2D Array
Description: For Loop, and Array
**/

//professor’s code
import java.util.Scanner;
import java.util.Random;

public class GridPractice
{

public static void main(String[] args)
{
//declarations
Scanner in = new Scanner(System.in);
Random  generator = new Random();
int [][] grid;    //un-instantiated grid
int size = 0;    //number of rows and columns

//get size of grid – no validation & instantiate
System.out.print(“Enter size of grid: “);
size = in.nextInt();
grid = new int[size][size];
//end of professor’s code

//beginning of my own code:

//fill grid with random number from 1..99
System.out.println();
for (int row=0; row<size; row++)
{
for (int col=0; col<size; col++)
{
grid[row][col] = generator.nextInt(100); //random numbers 0.99 – not 100
}
}

//print grid with formatting to align
System.out.println();
System.out.print(“Random values assigned to ” + size + ” by ” + size + ” grid.\n”);
for(int row = 0; row < size; row++)
{
for(int col = 0; col < size; col++)
{
System.out.printf(“%4d”, + grid[row][col]);
}
System.out.println();
}

//print each row sum
System.out.println();

int rowSum = 0;

System.out.print(“Row    Sum\n”);
System.out.print(“———-\n”);
for(int row = 0; row < size; row++)
{
for(int col = 0; col < size; col++)
{
rowSum += grid[row][col];
}

System.out.printf(“%3d”, + row);
System.out.printf(“%6d”, + rowSum);
System.out.println();
rowSum = 0;
}

//print each column sum
System.out.println();

int colSum = 0;

System.out.print(“Col    Sum\n”);
System.out.print(“———-\n”);
for(int col = 0; col < size; col++)
{
for(int row = 0; row < size; row++)
{
colSum += grid[row][col];
}

System.out.printf(“%3d”, + col);
System.out.printf(“%6d”, + colSum);
System.out.println();
colSum = 0;
}

//print two diagonal sums
int upperLeft2LowerRight = 0;
int upperRight2LowerLeft = 0;

for(int col = 0; col < grid[0].length; col++)
{
for(int row = 0; row < grid[0].length; row++)
{
if(row == col)
{
upperLeft2LowerRight += grid[row][col];
}
}
}

for(int row = 0; row < grid[0].length; row++)
{
for(int col = 0; col < grid[0].length; col++)
{
if(!(row == col))
{
upperRight2LowerLeft += grid[row][col];
}
}

}
System.out.print(“Diagonal from upper left to lower right is ” + upperLeft2LowerRight);
System.out.print(“\nDiagonal from upper right to lower left is ” + upperRight2LowerLeft);
}
}

You can test with any number from 1 to 99, and I will start with 2

Here is the output

2Darray

Java Programming: BankAccount/BankAccountTester

BankAccount Class Code:

public class BankAccount
{
//Data type
private double balance;
private double bankFees;
private int freeTrans;
private int transCount = 0;

//default constructor
public BankAccount()
{
balance = 0;
}

public BankAccount(double amount)
{
balance = amount;

}

//The method for withdraw
public void withdraw(double amount)
{
balance -= amount;
this.setTransNum();
}

//The method for deposit
public void deposit(double amount)
{
balance += amount;
this.setTransNum();
}

//the method for Transaction Fee
public void setTransFee(double amount)
{
bankFees = amount;

}

public void setTransNum()
{
transCount++;
}

//the method for Free Transactions
public void setNumFreeTrans(int amount)
{
freeTrans = amount;
}

public double monthlyCharge()
{
//return Math.max(transCount, freeTrans);

if(transCount > freeTrans)
{
balance -= ((transCount – freeTrans) * bankFees);
transCount = 0;
return balance;
}

else
{
transCount = 0;
return balance;
}

}

//Accessor method for balance
public double getBalance()
{
return balance;
}

}

Read the rest of this entry

Java Programming: First

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

System.out.println(“Testing”);
}

}
You can run the program by using JGRASP to see the output nor CMD (Command Shell):

Capture1

Capture

Programming Languages (Simple)

Java Programming:

//Author: Kevin Whetstone

public class Test
{
public static void main (String [] args)
{
System.out.print(“Hello People”);
}
}

Perl Programming:

#!/user/bin/perl
#Author: Kevin Whetstone

print (“Hello People”);

C++ Programming

#include <iostream>
#Author: Kevin T. Whetstone
using namespace hellworld;

int main ()
{
  cout << "Hello World!";
  return 0;
}

If you want other programming languages, please let me know so I can code the basic program to your preference. 

Wondering Something

I have two programming books but I am not sure which one I should do first, should I start learning programming from Java then C++ or the other way around C++ then Java? I hear Java is easier to learn but that is one opinion and I was hoping for more imput.

Thanks,

Kevin

Top 20 Most Popular Programming Languages Among Hacker News Readers

  1. Python (3044)
  2. Ruby (1718)
  3. JavaScript (1412)
  4. C 966
  5. C# 828
  6. PHP 662
  7. Java 551
  8. C++ 529
  9. Haskell 518
  10. Clojure 458
  11. CoffeeScript 361
  12. Objective C 326
  13. Lisp 321
  14. Perl 310
  15. Scala 233
  16. Scheme 190
  17. Other 188
  18. Erlang 162
  19. Lua 145
  20. SQL 101

They are the top numbers that hackers tend to use to hack any systems in the world and there is no way to secure them from learning the programming language because they can be taught by teachers, professors, friends, family members, or self-taught.

Source:

http://devopsangle.com/2012/04/06/top-20-most-popular-programming-languages-among-hacker-news-readers/

%d bloggers like this: