solve na ni?
solved na bro. heheh tnx ninyu
Ani ni siya jairoh...nganong 9 na siya.
9 is the value used to determine the kana nga Character kay digit na. (short cut).
Nganong 9 is sila ray gaboot, you could have used any value.
Nganong int type gigamit kay, ang switch case sa Java int raman dawaton.
This is for code writing purposes not for algorithm or analysis purposes.
Indicator variables in Java are usually int para ma take advantage ang mga conditional statements nila.
Lastly, makabantay ka CAPITAL na sila pirmi.
Codewise murag ingani.
So this is a short code to demonstrate the use of STATIC VARIABLES with INT values.Code:public class Dog{ public static final int IS_WALKING = 1; public static final int IS_URINATING = 3; public static final int IS_STAYING = 0; public int dogState = 0; public Dog(){ } public void stay(){ //do stopping algorithm dogState = IS_STAYING; } public void walk(){ //do walk algorithm dogState = IS_WALKING; } public void urinate() throws DogCommandException{ //MAO NI ANG SIMILAR SA ==9 IN THIS CASE ANG VARIABLE //ANG KANINDOT ANI SAYON SIYA BASAHON CODEWISE if(dogState == IS_WALKING){ GIGAMIT throw new DogCommandException("Cannot urinate while walking"); } else{ while(dogState == IS_URINATING){ //do urinate algorithm, raise legs, optional halhal dogstate = IS_URINATING; if(Kidneys.isEmpty(this)){ dogstate = IS_STAYING; } } } } } public class DogTrainer extends DogOwner{ public static void main(String args[]){ Dog myDog = DogTrainer.callDog(); myDog.bark(); myDog.walk(); try{ myDog.urinate(); } catch( DogCommandException e){ System.out.println(e.getMessage()); myDog.stay(); myDog.urinate(); } } }
Though nindot ta nga example kung SWITCH CASE kay kita gyud ang beauty niya.
This short code also demonstrates the use of try catch.
It is when you are expecting and/or attempting to contain an error to respond to it any manner.
In this case, pag tawag nako og URINATE while WALKING dili man pwede, so mo error siya expectedly.
so akong gi-try catch para madakpan ang error, unya i-execute ang sakto ng code.
Furthermore Exception Objects are used to show more information about any given Error or Exception.
Hope makatabang ni.
Last edited by Klave; 02-01-2012 at 09:03 PM.
TS, naa pa ko code oh...
simple and easy to read and understand...
i know if gamiton ni sya sa Data Structure hagbong ni tungod sa performance issue... but still it performs what is asked and dali ra sabton kay by methods man ni sya...
i hope maka tabang ni ako tampo nimo... hehehe
Code:import java.util.Scanner; import java.util.*; import javax.swing.*; public class Sample { private String input; private int max; private int min; private int stringLength; private int closestToZero; private int[] arrayNumber; private int numberArrayCount; public Sample() { input = JOptionPane.showInputDialog(null, "Enter string input"); numberArrayCount = 0; max = 0; min = 0; stringLength = input.length(); closestToZero = 0; arrayNumber = new int[stringLength]; Process(); } public void Process() { boolean isHyphen = false; for (int i = 0; i < stringLength; i++) { char c = input.charAt(i); if (c == '-') { isHyphen = true; continue; } if (Character.isDigit(c)) { if (isHyphen) { isHyphen = false; arrayNumber[numberArrayCount++] = Integer.parseInt(""+c) * -1; } else { arrayNumber[numberArrayCount++] = Integer.parseInt(""+c); } } } } public String toString() { StringBuffer sb = new StringBuffer(); sb.append("["); for(int i = 0; i < numberArrayCount; i++) { sb.append(arrayNumber[i]); if (i < (numberArrayCount-1)) { sb.append(", "); } } sb.append("]"); return "Input Value: " + input + "\nArray: " + sb.toString() + "\nLength of Array: " + numberArrayCount + "\nLength of String: " + stringLength + "\nMax: " + GetMax() + "\nMin: " + GetMin() + "\nClosest to Zero: " + GetClosestToZero(); } public int GetMax() { max = arrayNumber[0]; for (int i = 0; i < numberArrayCount; i++) { if (max < arrayNumber[i]) { max = arrayNumber[i]; } } return max; } public int GetMin() { min = arrayNumber[0]; for (int i = 0; i < numberArrayCount; i++) { if (min > arrayNumber[i]) { min = arrayNumber[i]; } } return min; } public int GetClosestToZero() { int temp1 = min; int temp2 = max; for (int i = 0; i < numberArrayCount; i++) { if (arrayNumber[i] < 0) { if (temp1 < arrayNumber[i]) { temp1 = arrayNumber[i]; } } else { if (temp2 > arrayNumber[i]) { temp2 = arrayNumber[i]; } } } if (Math.abs(temp1) <= temp2) { closestToZero = temp1; } else { closestToZero = temp2; } return closestToZero; } public static void main(String[] args) { Sample s = new Sample(); System.out.println(s); } }
Last edited by jinfreecs; 02-02-2012 at 12:38 AM.
@klave, stealthghost, thinkspace ug jinfreecs, tnx ninyu.
@jinfreecs murag mas limpyo ni imo dah, ako sang sabton ha. hehe
Similar Threads |
|