Tuesday, February 26, 2013
First Java Program
import java.util.Scanner;
public class Example FirstJavaProgram{
static final int NUMBER=12;
static Scanner console =new Scanner(System.in);
public static void main(String args[]){
int firstNum;
String secondNum;
firstNum=18;
System.out.println("Line 11: firstNum= "+firstNum);
System.out.println("Line 12: Enter an integer: ");
secondNum=console.nextInt();
System.out.println();
System.out.println("Line 15: secondNum= "+secondNum);
firstNum=firstNum+ NUMBER+ 2* secondNum;
System.out.println("Line 17: The new value of"+
"firstNum= " + firstNum);
}
}
Binary
/**
* @(#)midtermproject.java
*
*
* @ROGY T. ACOSTA, MSCS
* @version 1.00 2011/1/24
*/
import java.util.Scanner;
public class bi {
static Scanner console = new Scanner(System.in);
public static void main(String[] arg){
char ans;
do{
String binary;
System.out.println("Enter binary value:");
do {
binary = console.next();
}while(CheckInput(binary));
int[] placevalue = new int[binary.length()];
char[] digits = new char[binary.length()];
assignPlaceValue(placevalue);
getIndividualDigit(binary,digits);
int decimalval = binary2decimal(digits,placevalue);
System.out.println("Result: "+decimalval + " ");
System.out.println("Enter another binary number? [y/n]:->");
do {
ans = console.next().charAt(0);
}while(ans != 'n' && ans != 'N' && ans != 'Y' && ans != 'y' );
}while(ans == 'y' || ans == 'Y');
}
public static boolean CheckInput(String binaryinput){
for(int index = 0 ; index < binaryinput.length(); index++){
if ((binaryinput.charAt(index) != '1') && (binaryinput.charAt(index) != '0'))
return true;
}
return false;
}
public static void assignPlaceValue(int[] placeval){
placeval[0] = 1;
for(int index = 1 ; index < placeval.length; index++){
placeval[index] = placeval[index-1]*2;
}
}
public static void getIndividualDigit(String binaryvalue,char[] digitvalue){
for(int index = 0; index < binaryvalue.length(); index++){
digitvalue[index] = binaryvalue.charAt(index);
}
}
public static int binary2decimal(char[] digitvalue,int[] placeval){
int result = 0;
int digitplace = (digitvalue.length-1);
for(int index = 0; index < digitvalue.length; index++){
if (digitvalue[index] == '1')
{result = result + placeval[digitplace];
System.out.println("Placevalue: "+placeval[digitplace] +" index = "+index);
}
digitplace--;
}
return result;
}
public static int binary2octal(char[] digitvalue,int[] placeval){
int result = 0;
int digitplace = (digitvalue.length-1);
for(int index = 0; index < digitvalue.length; index++){
if (digitvalue[index] == '1')
{result = result + placeval[digitplace];
System.out.println("Placevalue: "+placeval[digitplace] +" index = "+index);
}
digitplace--;
}
return result;
}
}
Input Scanner
import java.util.Scanner;
public class sd
{
public static void main (String args[])
{
Scanner input = new Scanner(System.in);
Double df,er,td,io;
System.out.print("Enter first num: ");
df = input.nextDouble();
System.out.print("Enter second num: ");
er = input.nextDouble();
System.out.print("Enter third num: ");
td = input.nextDouble();
io = df + er * td;
System.out.print(df + " ert ");
System.out.printf("io");
}
}
Monday, February 25, 2013
hmtl project - Table
Stained Glass Club
<html> <head> <title>Stained Glass Club</title> <link rel = "stylesheet" type="text/css" href="stylesheet.css" /> <style type="text/css"> <!-- .style1 {font-size: 9pt} .style2 { font-size: 10pt; font-style: italic; } --> </style> </head> <body> <h1 align="center"><img src="../pic 1.png" width="793" height="198"></h1> <h1 align="center"><strong>Calendar</strong></h1> <p align="justify"> The Stained Glass Club offers many classes in different stained glass techniques. You must take the Introduction class to learn how to use the equipment before taking any other classes. All classes are free, but reservations must be made one week in advance by calling 612-555-1000. <table width="749" height="201" border="1" align="center"> <tr> <th width="182" scope="col">Date</th> <th width="176" scope="col">Time</th> <th width="177" scope="col">Instructor</th> <th width="203" scope="col">Topic</th> </tr> <tr> <td>January 5, 2006 </td> <td>7:00 <span class="style1">p.m </span></td> <td>Tanya Wattigney </td> <td>Introduction</td> </tr> <tr> <td>January 12, 2006 </td> <td>11:00 <span class="style1">p.m </span></td> <td>Barbara Ryan </td> <td>Fusing</td> </tr> <tr> <td>January 17, 2006</td> <td>2:00 <span class="style1">p.m</span> </td> <td>Bill Frasca </td> <td>Fusing</td> </tr> <tr> <td>January 21, 2006</td> <td>1:30 <span class="style1">p.m </span></td> <td>La Tonya LaBelle </td> <td>Mosaic</td> </tr> <tr> <td>January 23, 2006</td> <td>10:00 <span class="style1">a.m</span> </td> <td>Doug Henderson </td> <td>Mosaic</td> </tr> <tr> <td>January 30, 2006</td> <td>3:00 <span class="style1">p.m</span></td> <td>Joanie Gross </td> <td>Introduction</td> </tr> </table> <p align="center" class="style2">Please phone ahead for registration. </p> </body> </html>
html project
Stained Glass Club
<htm><head> <title>Stained Glass Club</title> <link rel="stylesheet" type="text/css" href =styles1.css"/ </head> <body> <h1 align="center" style="font-style:strong; font-size: 18pt; font-family:verdana; color:blue"><img src="../pic 1.png" width="793" height="189">welcome</h1> <p align="justify">Welcome to the stained Glass Club web site. the goal of this site is to distribute information on stained glass related topics. The Stained Glass Club is a great place where you can learn, meet other people with an interest in stained glass, and have fun</p> <p align="justify">With this web-site, we are happy to provide you with information on our state-of-the-art stained glass facilities, our list of courses, and links to other valuable stained glass web sites. </p> <p align="justify">Please select your option from the links on the left. Let us know at <strong><a href= "mailto:sgc@isp.com"> Stained Glass Club</a><strong> if you have any questions. Complete the form on the Contact page to share ideas on other classes or services we should offer.</p> <p align="justify" style= "font-style: italic; font-size: 8pt">This web site is maintained by our<strong><a href = "mailto: sgsupport@isp.com">support staff </a></strong>. Please e-mail all site questions and comments to us. <br />Last updated December 10, 2005.</p> </body> </html>
String To Upper Case
import javax.swing.JOptionPane;
public class we {
public static void main(String[] args) {
new we().stringToUpperCase();
}
public void stringToUpperCase() {
String a ;
a=JOptionPane.showInputDialog("Enter Uppercase letters");
a = a.toUpperCase();
JOptionPane.showMessageDialog(null,a,"answer",JOptionPane.PLAIN_MESSAGE);
System.exit( 0 );
}
}
Menu using Switch Case
import java.util.Scanner;
public class m {
public static void main(String args[]){
Scanner input=new Scanner(System.in);
int g;
int f;
int h;
String a;
char d;
System.out.println(" MENU ");
System.out.println("1 - PAYROLL " +
"\n2 - GRADE " +
"\n3 - PAYCHECK " );
do{
System.out.print("Choose a number: " );
g = input.nextInt();
System.out.println();
String name;
int employee_number;
double payRate;
double hours;
double weeklyPay;
switch (g){
case 1:
System.out.print("Enter name: ");
name = input.next();
System.out.print("Enter employee_number: ");
employee_number = input.nextInt();
System.out.print("Enter payrate: ");
payRate = input.nextDouble();
System.out.print("Enter hours: ");
hours = input.nextDouble();
weeklyPay = payRate * hours;
System.out.print(name + " earned ");
System.out.printf("$ %.2fn", weeklyPay);
System.out.println();
break;
case 2:
String df;
int total;
int taxAmount;
int totalMeal;
int tip;
int overall;
System.out.println("Enter the total bill ");
total =input.nextInt();
System.out.println("The taxed amount is" );
taxAmount = (int )6.75/100 *total;
totalMeal = taxAmount + total;
System.out.println("The total meal: " + totalMeal);
tip = 15/100 * totalMeal;
System.out.println("The tip amount: " + tip);
overall = totalMeal + tip;
System.out.println("The total bill: " + overall);
System.out.println();
break;
case 3:
double baseSalary,
noOfServiceYears,
totalSales,
additionalBonus,
paycheck,
bonus;
System.out.println("Enter the salary base" );
baseSalary = input.nextInt();
System.out.println("Enter the no of service years" );
noOfServiceYears = input.nextInt();
if (noOfServiceYears<= 5)
{bonus = 10 * noOfServiceYears;
System.out.println("bonus\n" + bonus);}
else
{bonus = 20 * noOfServiceYears;
System.out.println("bonus\n" + bonus);}
System.out.println("Enter the Total Sales");
totalSales = input.nextInt();
if (totalSales <= 5000)
{additionalBonus = 0;
System.out.println("additional bonus\n" + additionalBonus);}
else if (totalSales >= 5000 && totalSales <= 1000)
{additionalBonus = totalSales * 0.03;
System.out.println("additional bonus\n" + additionalBonus);}
else
{additionalBonus = totalSales * 0.06;
System.out.println("additional bonus\n" + additionalBonus);}
paycheck = baseSalary + bonus + additionalBonus;
System.out.println("Pay Check\n"+ paycheck);
System.out.println();
}
System.out.println("Do you want to continue press Y/N: ");
d= input.next().charAt(0);
switch(d){
case 'y':
case 'Y': System.out.println();
break;
case 'n':
case 'N': System.out.println("THANK YOU!!");
g=d;
break;
}
} while(g!=d);
}
}
Decimal To Binary Converter
import java.util.Scanner;
public class DecimalToBinaryConverter {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int decimalNumber;
String binaryNumber;
System.out.print("Enter a positive integer: ");
decimalNumber = in.nextInt();
if (decimalNumber <= 0)
System.out.println("ERROR: entered integer is nonpositive.");
else {
binaryNumber = "";
while (decimalNumber != 0) {
if (binaryNumber.length() % 5 == 0)
binaryNumber = " " + binaryNumber;
binaryNumber = (decimalNumber % 2) + binaryNumber;
decimalNumber /= 2;
}
System.out.println("Binary: " + binaryNumber);
}
}
}
Decimal to Binary
import java.util.*;
public class ConversionofDecimaltoBinary
{
public static void main(String[] args)
{
Scanner keyboardin = new Scanner(System.in);
int [] output = new int [32];
int x;
int digit;
int base =2;
int digitno =0;
x = keyboardin.nextInt();
while (x>0)
{
digit = x%base;
output[digitno] = digit;
digitno++;
x = x%base;
for(int i = digitno - 1; i>=0; i--);
System.out.println(digit[i]);
}
}
}
Sunday, February 24, 2013
Test Seq Search
import java.util.Scanner;
public class testSeqSearch {
static Scanner console = new Scanner(System.in);
static final int Array_Size=10;
public static void main(String[] args) {
int[] intList = new int[Array_Size];
int number;
int index;
System.out.println("L 4:Enter "+Array_Size+" integers.");
for(index=0; index<Array_Size; index++)
intList[index]=console.nextInt();
System.out.println();
System.out.println("L 8:Enter the number to be searched:");
number=console.nextInt();
System.out.println();
index = seqSearch(intList, Array_Size, number);
if (index !=-1)
System.out.println("L 13:"+number+"is found at position "
+index);
else
System.out.println("L 15:"+number+"is not in the list.");
}
public static int seqSearch(int[] list, int listLength, int searchItem){
int loc;
for(loc=0; loc<listLength; loc++)
if(list[loc]== searchItem)
return loc;
return -1;
}
}
Test Selection Sort
public class Testselectionsort {
public static void main(String[] args) {
int list[]={2, 56, 34, 25, 73, 46, 89, 10, 5, 16};
int i;
selectionSort(list, 10);
System.out.println("After sorting, the list elements are:");
for(i=0; i<10; i++)
System.out.print(list[i]+" ");
System.out.println();
}
public static void selectionSort(int[] list, int listLength){
int index;
int smallestIndex;
int minIndex;
int temp;
for(index=0; index<listLength-1; index++){
smallestIndex = index;
for(minIndex = index+1; minIndex<listLength;minIndex++);
temp = list[smallestIndex];
list[smallestIndex]=list[index];
list[index]=temp;
}
}
}
Test Bubble Sort
public class Testbubblesort {
public static void main(String[] args) {
int list[]={2, 56, 34, 25, 73, 46, 89,
10, 5, 16};
int i;
bubbleSort(list, 10);
System.out.println("After sorting, the list elements are:");
for(i=0; i<10; i++)
System.out.print(list[i]+" ");
System.out.println();
}
public static void bubbleSort(int list[], int listLength){
int temp;
int counter, index;
for (counter = 0; counter<listLength -1; counter++){
for(index=0; index<listLength -1 - counter; index++)
if (list[index]>list[index+1]){
temp = list[index];
list[index]=list[index+1];
list[index+1]=temp;
}
}
}
}
Binary
/**
* @(#)midtermproject.java
*
*
* @ROGY T. ACOSTA, MSCS
* @version 1.00 2011/1/24
*/
import java.util.Scanner;
public class bi {
static Scanner console = new Scanner(System.in);
public static void main(String[] arg){
char ans;
do{
String binary;
System.out.println("Enter binary value:");
do {
binary = console.next();
}while(CheckInput(binary));
int[] placevalue = new int[binary.length()];
char[] digits = new char[binary.length()];
assignPlaceValue(placevalue);
getIndividualDigit(binary,digits);
int decimalval = binary2decimal(digits,placevalue);
System.out.println("Result: "+decimalval + " ");
System.out.println("Enter another binary number? [y/n]:->");
do {
ans = console.next().charAt(0);
}while(ans != 'n' && ans != 'N' && ans != 'Y' && ans != 'y' );
}while(ans == 'y' || ans == 'Y');
}
public static boolean CheckInput(String binaryinput){
for(int index = 0 ; index < binaryinput.length(); index++){
if ((binaryinput.charAt(index) != '1') && (binaryinput.charAt(index) != '0'))
return true;
}
return false;
}
public static void assignPlaceValue(int[] placeval){
placeval[0] = 1;
for(int index = 1 ; index < placeval.length; index++){
placeval[index] = placeval[index-1]*2;
}
}
public static void getIndividualDigit(String binaryvalue,char[] digitvalue){
for(int index = 0; index < binaryvalue.length(); index++){
digitvalue[index] = binaryvalue.charAt(index);
}
}
public static int binary2decimal(char[] digitvalue,int[] placeval){
int result = 0;
int digitplace = (digitvalue.length-1);
for(int index = 0; index < digitvalue.length; index++){
if (digitvalue[index] == '1')
{result = result + placeval[digitplace];
System.out.println("Placevalue: "+placeval[digitplace] +" index = "+index);
}
digitplace--;
}
return result;
}
public static int binary2octal(char[] digitvalue,int[] placeval){
int result = 0;
int digitplace = (digitvalue.length-1);
for(int index = 0; index < digitvalue.length; index++){
if (digitvalue[index] == '1')
{result = result + placeval[digitplace];
System.out.println("Placevalue: "+placeval[digitplace] +" index = "+index);
}
digitplace--;
}
return result;
}
}
Various String Methods
public class variousStringMethods {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String sentence,
s1,
s2,
s3;
int index;
sentence = "Celebrate the season of gift-giving";
System.out.println("sentence = \"" + sentence + "\"");
System.out.println("the length of sentence = "
+ sentence.length());
System.out.println(" sentence = \"" + sentence + "\"");
System.out.println("the character at index 16 in "
+ "sentence = "
+ sentence.charAt(17));
System.out.println("the character at index 16 in "
+ "sentence = "
+ sentence.charAt(17));
System.out.println("the index of first s in "
+ "sentence = "
+ sentence.indexOf('s'));
System.out.println("the index of for in "
+ "sentence = "
+ sentence.indexOf("for"));
System.out.println("sentence in uppercase = \n"
+ " \""
+ sentence.toUpperCase() + "\"");
index = sentence.indexOf(" season ");
s1 = sentence.substring(index, index + 15);
System.out.println("s1 = \""
+ s1 +"\"");
s2 = "Super" + s1;
System.out.println("s2 = \""
+ s2 + "\"");
s3 = sentence.replace('s', 'S');
System.out.println("s3 = \""
+ s3 + "\"");
}
}
Simple Calculator 4
import java.util.Scanner;
public class calculator3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1,
num2,
ans;
char ope;
System.out.print("ENTER THE FIRST NUMBER: " );
num1 = input.nextInt();
System.out.print("ENTER THE SECOND NUMBER " );
num2 = input.nextInt();
System.out.print("ENTER THE OPERATOR +,-,*,/ : " );
ope = input.next().charAt(0);
System.out.print(num1 + " " + ope + " " + num2 + " = ");
if (ope =='+'){
ans = num1 + num2 ;
System.out.print(ans);}
else if (ope =='-'){
ans = num1 - num2 ;
System.out.print(ans);}
else if (ope =='*'){
ans = num1 * num2 ;
System.out.print(ans);}
else if (ope == '/'){
ans = num1 / num2 ;
System.out.print(ans);}
}
}
Simple Calculator 3
import java.util.Scanner;
public class calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1,
num2,
ans;
char ope;
System.out.print("ENTER THE FIRST NUMBER: " );
num1 = input.nextInt();
System.out.print("ENTER THE SECOND NUMBER " );
num2 = input.nextInt();
System.out.print("ENTER THE OPERATOR +,-,*,/ : " );
ope = input.next().charAt(0);
System.out.print(num1 + " " + ope + " " + num2 + " = ");
int add = num1 + num2;
int minus = num1 - num2;
int multiply = num1 * num2;
int divide = num1 / num2;
switch (ope)
{
case '+':
ans = num1 + num2 ;
System.out.println( add);
break;
case '-':
ans = num1 - num2 ;
System.out.println( minus);
break;
case '*':
ans = num1 * num2 ;
System.out.println( multiply);
break;
case '/':
ans = num1 / num2 ;
System.out.println( divide);
}
}
}
Simple Calculator 2
import java.util.Scanner;
public class caculator1
{
public static void main (String[] args)
{
//INPUT
Scanner kybd = new Scanner(System.in);
int number1;
int number2;
String operator;
char operator1;
System.out.println("Enter equation");
number1 = kybd.nextInt();
operator = kybd.next();
number2 = kybd.nextInt();
operator1 = operator.charAt(0);
int add = number1 + number2;
int minus = number1 - number2;
int multiply = number1 * number2;
int divide = number1 / number2;
switch (operator1)
{
case '+':
System.out.println("The answer is: " + add);
break;
case '-':
System.out.println("The answer is: " + minus);
break;
case '*':
System.out.println("The answer is: " + multiply);
break;
case '/':
System.out.println("The answer is: " + divide);
}
}
}
Hexagonal To Binary
import java.util.Scanner;
public class JavaHexToBin
{
public static void main(String[]args)
{
String[]hex={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
String[]binary={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
Scanner s=new Scanner(System.in);
System.out.print("Put hex value : ");
String userInput=s.next();
String result="";
for(int i=0;i<userInput.length();i++)
{
char temp=userInput.charAt(i);
String temp2=""+temp+"";
for(int j=0;j<hex.length;j++)
{
if(temp2.equalsIgnoreCase(hex[j]))
{
result=result+binary[j];
}
}
}
System.out.println("IT'S BINARY IS : "+result);
}
}
Sample For Loop 5
public class sample {
public static void main(String[] args) {
for(int i=1; i<=5; i++)
{
for(int j=i; j<=i+5; j++)
System.out.print(j + " ");
System.out.println();
}
}
}
Sample For Loop 4
public class act6 {
public static void main(String[] args) {
for(int i=1; i>=6; i--)
{
for(int j=1; j>=i; j--)
System.out.print(" * ");
System.out.println();
}
for(int i=5; i>=1; i--)
{
for(int j=5; j>=i; j--)
System.out.print(" * ");
System.out.println();
}
}
}
Sample For Loop 3
public class act5 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer("AB");
for(int i=2; i<=7; i++)
{
System.out.print(a);
for(int j=i; j<=i+2; j++)
System.out.print(a);
a.reverse();
System.out.println();
}
}
}
Sample For Loop 2
public class act4 {
public static void main(String[] args) {
for(int i=1; i<=6; i++)
{
for(int j=1; j<=i; j++)
System.out.print(" * ");
System.out.println();
}
for(int i=1; i<=5; i++)
{
for(int j=i; j<=5; j++)
System.out.print(" * ");
System.out.println();
}
}
}
Sample For Loop
public class act3 {
public static void main(String[] args) {
for(int i=1; i<=6; i++)
{
for(int j=1; j<=i; j++)
System.out.print(" * ");
System.out.println();
}
for(int i=1; i<=5; i++)
{
for(int j=i; j<=5; j++)
System.out.print(" * ");
System.out.println();
}
}
}
Sample While Loop 2
public class act2 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer("AB");
int i=2;
while( i<=6 )
{
System.out.print(a);
int j=i;
while( j<=i+2 )
{
System.out.print(a);
a.reverse();
j++;
}
i++;
System.out.println();
}
}
}
sample While loop
public class act1 {
public static void main(String[] args) {
int d=1;
while( d<=5)
{
d++;
int j=1;
while( j<=d)
{
System.out.print(" * ");
j++;
}
System.out.println();
}
int i=1;
while( i<=5 )
{
i++;
int j=i;
while( j<=6 )
{
System.out.print(" * ");
j++;
}
System.out.println();
}
}
}
Traffic Lights in java
object oriented programming
import javax.swing.JFrame;
public class TrafficControlApp {
/**
* @param args
*/
public static void main(String[] args) {
TrafficLight application = new TrafficLight();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class TrafficFixtureForward{
public int locateX;
public String direction;
public TrafficFixtureForward(String direction, int locateX){
this.locateX = locateX;
this.direction = direction;
}
public void trafficLight(Graphics g, int state){
g.setColor(Color.black);
g.setFont(new Font("Arial",Font.BOLD, 12));
g.drawString(direction, (locateX+4), 250);
switch(state){
case 1: g.setColor(Color.red);
g.fillOval(locateX,30,50,50);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.black);
g.fillOval(locateX,150,50,50);
break;
case 2: g.setColor(Color.black);
g.fillOval(locateX,30,50,50);
g.setColor(Color.yellow);
g.fillOval(locateX,90,50,50);
g.setColor(Color.black);
g.fillOval(locateX,150,50,50);
break;
case 3: g.setColor(Color.black);
g.fillOval(locateX,30,50,50);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.green);
g.fillOval(locateX,150,50,50);
break;
case 4: g.setColor(Color.black);
g.fillOval(locateX,30,50,50);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.green);
g.fillOval(locateX,150,50,50);
break;
}
}
public void delay(int delay){
try
{
Thread.sleep(delay); // do nothing for 1000 miliseconds (1 second)
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class TrafficFixtureTurn{
public int locateX;
public String direction;
public TrafficFixtureTurn(String direction, int locateX){
this.locateX = locateX;
this.direction = direction;
}
public void trafficLight(Graphics g,int state){
g.setColor(Color.black);
g.setFont(new Font("Arial",Font.BOLD, 12));
g.drawString(direction, (locateX+4), 250);
switch(state){
case 1: g.setColor(Color.black);
g.fillOval( locateX,30,50,50);
g.setColor(Color.red);
g.setFont(new Font("Arial",Font.BOLD, 40));
g.drawString("<", (locateX+10), 69);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.black);
g.fillOval(locateX,150,50,50);
break;
case 2: g.setColor(Color.black);
g.fillOval(locateX,30,50,50);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.yellow);
g.setFont(new Font("Arial",Font.BOLD, 40));
g.drawString("<", (locateX+10), 129);
g.setColor(Color.black);
g.fillOval(locateX,150,50,50);
break;
case 3: g.setColor(Color.black);
g.fillOval(locateX,30,50,50);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.black);
g.fillOval(locateX,150,50,50);
g.setColor(Color.green);
g.setFont(new Font("Arial",Font.BOLD, 40));
g.drawString("<", (locateX+10), 189);
break;
case 4: g.setColor(Color.black);
g.fillOval(locateX,30,50,50);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.black);
g.fillOval(locateX,150,50,50);
g.setColor(Color.green);
g.setFont(new Font("Arial",Font.BOLD, 40));
g.drawString("<", (locateX+10), 189);
break;
}
}
public void delay(int delay){
try
{
Thread.sleep(delay); // do nothing for 1000 miliseconds (1 second)
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
import javax.swing.JFrame;
public class TrafficControlApp {
/**
* @param args
*/
public static void main(String[] args) {
TrafficLight application = new TrafficLight();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class TrafficFixtureForward{
public int locateX;
public String direction;
public TrafficFixtureForward(String direction, int locateX){
this.locateX = locateX;
this.direction = direction;
}
public void trafficLight(Graphics g, int state){
g.setColor(Color.black);
g.setFont(new Font("Arial",Font.BOLD, 12));
g.drawString(direction, (locateX+4), 250);
switch(state){
case 1: g.setColor(Color.red);
g.fillOval(locateX,30,50,50);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.black);
g.fillOval(locateX,150,50,50);
break;
case 2: g.setColor(Color.black);
g.fillOval(locateX,30,50,50);
g.setColor(Color.yellow);
g.fillOval(locateX,90,50,50);
g.setColor(Color.black);
g.fillOval(locateX,150,50,50);
break;
case 3: g.setColor(Color.black);
g.fillOval(locateX,30,50,50);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.green);
g.fillOval(locateX,150,50,50);
break;
case 4: g.setColor(Color.black);
g.fillOval(locateX,30,50,50);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.green);
g.fillOval(locateX,150,50,50);
break;
}
}
public void delay(int delay){
try
{
Thread.sleep(delay); // do nothing for 1000 miliseconds (1 second)
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class TrafficFixtureTurn{
public int locateX;
public String direction;
public TrafficFixtureTurn(String direction, int locateX){
this.locateX = locateX;
this.direction = direction;
}
public void trafficLight(Graphics g,int state){
g.setColor(Color.black);
g.setFont(new Font("Arial",Font.BOLD, 12));
g.drawString(direction, (locateX+4), 250);
switch(state){
case 1: g.setColor(Color.black);
g.fillOval( locateX,30,50,50);
g.setColor(Color.red);
g.setFont(new Font("Arial",Font.BOLD, 40));
g.drawString("<", (locateX+10), 69);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.black);
g.fillOval(locateX,150,50,50);
break;
case 2: g.setColor(Color.black);
g.fillOval(locateX,30,50,50);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.yellow);
g.setFont(new Font("Arial",Font.BOLD, 40));
g.drawString("<", (locateX+10), 129);
g.setColor(Color.black);
g.fillOval(locateX,150,50,50);
break;
case 3: g.setColor(Color.black);
g.fillOval(locateX,30,50,50);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.black);
g.fillOval(locateX,150,50,50);
g.setColor(Color.green);
g.setFont(new Font("Arial",Font.BOLD, 40));
g.drawString("<", (locateX+10), 189);
break;
case 4: g.setColor(Color.black);
g.fillOval(locateX,30,50,50);
g.setColor(Color.black);
g.fillOval(locateX,90,50,50);
g.setColor(Color.black);
g.fillOval(locateX,150,50,50);
g.setColor(Color.green);
g.setFont(new Font("Arial",Font.BOLD, 40));
g.drawString("<", (locateX+10), 189);
break;
}
}
public void delay(int delay){
try
{
Thread.sleep(delay); // do nothing for 1000 miliseconds (1 second)
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
Subscribe to:
Comments (Atom)