Hi. i'm came upon this java script and i find it extremely interesting. Firstly because im a noob. but also i wanna ask some questions. Don't mind please do reply! thanks.
/**
* @(#)ComputeBPR.java
*
*
* @author
* @version 1.00 2008/1/24
*/
public class ComputeBPR {
static int systollic;//delcare Systollic
static String name;
static int diastollic;//declare Diastolic
static String printout;
static int verify;
static String[]array = new String[10];//delcare array of size 10
static int counter ;
public static void main(String[] args)throws Exception {
int choice;
do{
System.out.println("********* BPR Program *********");
System.out.println("1) Check your BPR Status");
System.out.println("2) Tactics to Lower BPR");
System.out.println("3) Exit program
");
System.out.println("***********************************");
System.out.print("Please select your choice <1-3> : ");
choice = CspInput.readInt();
switch(choice){ // switch case (choice)
case 1:choice1();
break;
case 2:choice2();
break;
case 3:choice3(array,counter);
break;
default:System.out.println("Please enter only 1,2,3: ");
}
}while(choice != 3);
}// end of main method
static void choice1()throws Exception{ // method declaration for choice1()
System.out.println("");
System.out.println(" Blood Pressure Range <BPR>");
System.out.println(" ==========================");
System.out.println("Please enter your name: ");
name = CspInput.readString();
System.out.println("Enter your systollic in (mm Hg): ");
systollic = CspInput.readInt();
System.out.println("Enter your diastollic in (mm Hg): ");
diastollic = CspInput.readInt();
System.out.println("Have you keyed in correctly <Y/N>? : ");
char verify = (char)System.in.read();
System.in.read();
System.in.read();
if((verify == 'Y') || (verify == 'y'))
{
if(((211<=systollic)&&(systollic<=230))&&((121<=diastollic)&&(diastollic<=140)))//211<sys<230 && 121<dia<140
printout= "You have Very Severe Hypertension.";
else if(((181<=systollic)&&(systollic<=210))&&((111<=diastollic)&&(diastollic<=120)))//181<sys<210 && 111<dia<120
printout= "You have Severe Hypertension";
else if(((161<=systollic)&&(systollic<=180))&&((101<=diastollic)&&(diastollic<=110)))//161<sys<180 && 101<dia<110
printout= "You have Moderate Hypertension.";
else if(((141<=systollic)&&(systollic<=160))&&((91<=diastollic)&&(diastollic<=100)))//141<sys<160 && 91<dia<100
printout= "You have Mild Hypertension";
else if(((91<=systollic)&&(systollic<=140))&&((61<=diastollic)&&(diastollic<=90)))//91<sys<140 && 61<dia<90
printout= "Your blood pressure is normal.";
else if(((51<=systollic)&&(systollic<=90))&&((31<=diastollic)&&(diastollic<=60)))//51<syst<90 && 31<dia<60
printout= "You have low blood pressure";
else
printout= "Your blood pressure is abnormal";
System.out.println("");
System.out.println("");
System.out.println(" Name\t\t\t Systollic\t\t\t Diastollic\t\t BPR Status");
System.out.println(" ----\t\t\t ---------\t\t\t ----------\t\t ------------");
System.out.println(name+"\t\t\t"+systollic+"\t\t\t\t"+diastollic+"\t\t\t\t"+printout);
System.out.println("");
array[counter] = name+"\t\t\t"+systollic+"\t\t\t\t"+diastollic+"\t\t\t\t"+printout;
counter += 1;
}
else if ((verify == 'N') || (verify == 'n'))
choice1();
else
{
System.out.println("Please re-enter either <Y/N>");
choice1();
}
}
static void choice2(){ // method declaration for choice2()
System.out.println("*** Blood Pressure Lowering Tactics *** ");
System.out.println(" Stop Smoking");
System.out.println(" Reduce Weight");
System.out.println(" Exerice");
System.out.println(" Low salt");
System.out.println(" Low Protein");
System.out.println(" Mild Sedation");
System.out.println(" No Caffein");
System.out.println(" Sufficient Rest");
System.out.println(" Dun Oversleep ");
}
static void choice3(String[]array,int counter){ // method declaration for choice3()
System.out.println(" Name\t\t\t Systollic\t\t\t Diastollic\t\t BPR Status");
System.out.println(" ----\t\t\t ---------\t\t\t ----------\t\t ----------------------");
for(int i = 0;i<counter;i++)
System.out.println(array[i]);
System.out.println("Thank you for using the BPR program. Good Bye...");
}
}//end class
As above in BOLD and underlined. Can someone tell me what are those for? like throws exception. What is it for?
Or under choice 3, the (array,counter) thing. what is it for? and what (array[i]) is for? do enlighten me! thanks!
Google "learn java", with or without the quotation marks.
But i simply can't get what the internet is saying. its in a language too chim for me to understand. So if the guys here can help, please do.
Thanks.
......
If too chim, might as well don't learn.
Array - An array is a collection of variables of the same type.
To keep on generating a thousand and one variables isn't fun:
int a = 0;
int b = 1;
int c = 2;
int d = 3;
int e = 4;
So it's grouped into an array, like this:
int [] digits = new int [5];
Arrays start counting from zero. 0, 1, 2, 3, 4 = total 5 numbers.
Exception is for handling errors.
if everything chim then don't learn. So what is the word "learn" for? Right? lol
Ok. thanks. But About exception, throws exception simply means ignoring any errors right?
LOL
No, not ignoring errors.
handling exceptions means what do you wan to do when something unexpected occurs such as, you're expecting to read a important data which somehow got lost and came out null which result in nullpointer exception. so throw exception actually means ignore any errors and continue with the flow. catching exception is whereby you define another set of flow in the event something unexpected occurs.
I thought it was a bot post like the one I saw on another SG-based forum until you replied. Anyway, firstly, this is no JavaScript, but Java, which both are totally unrelated to each other.
I'll elaborate more on the exceptions thingie. It's used to interrupt a program flow when something is not right and the following statements need to be skipped which may otherwise cause the entire program to malfunction. For example (the code below is in OO-PHP, but its syntax is quite similar to Java's):
class Girl {
private $anger = 0;
private $timeOfTheMonth = 0;
public function __construct($day) {
$this->timeOfTheMonth = mt_rand(1, 31);
if ($this->day == $this->timeOfTheMonth) {
incrementAnger(50);
}
}
private function incrementAnger($add) {
$this->anger += $add;
if ($this->anger > 100) throw new Exception("Girl is too angry.");
}
public function dating($gift, $guyAppearance, $goDutch) {
try {
if ($gift == 'none') {
incrementAnger(80);
}
if ($guyAppearance < 50) {
incrementAnger(40);
}
if ($goDutch == true) {
incrementAnger(40);
}
goMakan();
} catch (exception $e) {
if ($e->getMessage() == 'Girl is too angry.') {
tellsTheGuy("I'm not feeling well today. I need to go home now.");
}
}
}
}
http://java.sun.com/docs/books/tutorial/essential/exceptions/throwing.html
So, before goMakan(), in any event of an incrementAnger(), the anger level is incremented and checked and if it exceeds 100, an exception is thrown and any following statement within the try{} block will be skipped. Script execution will continue from the catch{} block where the exception is caught. If there is no exception thrown, the statements within the catch{} block will not be ran.
It's not like the errors are ignored or something, otherwise the girl may very well fall in love with an ugly guy who gives her no present and asks her to pay for the whole dinner on the first date even when she was having PMS.
Originally posted by juantan:Hi. i'm came upon this java script and i find it extremely interesting. Firstly because im a noob. but also i wanna ask some questions. Don't mind please do reply! thanks.
/**
* @(#)ComputeBPR.java
*
*
* @author
* @version 1.00 2008/1/24
*/
public class ComputeBPR {
static int systollic;//delcare Systollic
static String name;
static int diastollic;//declare Diastolic
static String printout;
static int verify;
static String[]array = new String[10];//delcare array of size 10
static int counter ;
public static void main(String[] args)throws Exception {
int choice;
do{
System.out.println("********* BPR Program *********");
System.out.println("1) Check your BPR Status");
System.out.println("2) Tactics to Lower BPR");
System.out.println("3) Exit program ");
System.out.println("***********************************");
System.out.print("Please select your choice <1-3> : ");
choice = CspInput.readInt();
switch(choice){ // switch case (choice)
case 1:choice1();
break;
case 2:choice2();
break;
case 3:choice3(array,counter);
break;
default:System.out.println("Please enter only 1,2,3: ");
}
}while(choice != 3);
}// end of main method
static void choice1()throws Exception{ // method declaration for choice1()
System.out.println("");
System.out.println(" Blood Pressure Range <BPR>");
System.out.println(" ==========================");
System.out.println("Please enter your name: ");
name = CspInput.readString();
System.out.println("Enter your systollic in (mm Hg): ");
systollic = CspInput.readInt();
System.out.println("Enter your diastollic in (mm Hg): ");
diastollic = CspInput.readInt();
System.out.println("Have you keyed in correctly <Y/N>? : ");
char verify = (char)System.in.read();
System.in.read();
System.in.read();
if((verify == 'Y') || (verify == 'y'))
{
if(((211<=systollic)&&(systollic<=230))&&((121<=diastollic)&&(diastollic<=140)))//211<sys<230 && 121<dia<140
printout= "You have Very Severe Hypertension.";
else if(((181<=systollic)&&(systollic<=210))&&((111<=diastollic)&&(diastollic<=120)))//181<sys<210 && 111<dia<120
printout= "You have Severe Hypertension";
else if(((161<=systollic)&&(systollic<=180))&&((101<=diastollic)&&(diastollic<=110)))//161<sys<180 && 101<dia<110
printout= "You have Moderate Hypertension.";
else if(((141<=systollic)&&(systollic<=160))&&((91<=diastollic)&&(diastollic<=100)))//141<sys<160 && 91<dia<100
printout= "You have Mild Hypertension";
else if(((91<=systollic)&&(systollic<=140))&&((61<=diastollic)&&(diastollic<=90)))//91<sys<140 && 61<dia<90
printout= "Your blood pressure is normal.";
else if(((51<=systollic)&&(systollic<=90))&&((31<=diastollic)&&(diastollic<=60)))//51<syst<90 && 31<dia<60
printout= "You have low blood pressure";
else
printout= "Your blood pressure is abnormal";
System.out.println("");
System.out.println("");
System.out.println(" Name\t\t\t Systollic\t\t\t Diastollic\t\t BPR Status");
System.out.println(" ----\t\t\t ---------\t\t\t ----------\t\t ------------");
System.out.println(name+"\t\t\t"+systollic+"\t\t\t\t"+diastollic+"\t\t\t\t"+printout);
System.out.println("");
array[counter] = name+"\t\t\t"+systollic+"\t\t\t\t"+diastollic+"\t\t\t\t"+printout;
counter += 1;
}
else if ((verify == 'N') || (verify == 'n'))
choice1();
else
{
System.out.println("Please re-enter either <Y/N>");
choice1();
}
}
static void choice2(){ // method declaration for choice2()
System.out.println("*** Blood Pressure Lowering Tactics *** ");
System.out.println(" Stop Smoking");
System.out.println(" Reduce Weight");
System.out.println(" Exerice");
System.out.println(" Low salt");
System.out.println(" Low Protein");
System.out.println(" Mild Sedation");
System.out.println(" No Caffein");
System.out.println(" Sufficient Rest");
System.out.println(" Dun Oversleep ");
}
static void choice3(String[]array,int counter){ // method declaration for choice3()
System.out.println(" Name\t\t\t Systollic\t\t\t Diastollic\t\t BPR Status");
System.out.println(" ----\t\t\t ---------\t\t\t ----------\t\t ----------------------");
for(int i = 0;i<counter;i++)
System.out.println(array[i]);
System.out.println("Thank you for using the BPR program. Good Bye...");
}
}//end class
As above in BOLD and underlined. Can someone tell me what are those for? like throws exception. What is it for?
Or under choice 3, the (array,counter) thing. what is it for? and what (array[i]) is for? do enlighten me! thanks!
This is not javascript dude.
This is Java. Its a heavyweight compared to javascript.
Choice 3 is to show the all strings stored in the variable "array." Choose a better variable name. U don't have to name something "array" just to reflect that its an array. String[] Diagnosis; is more meaningful.
2nd, avoid using Arrays when not all the array size will be used up. Use Vectors as the size can grow.
3rd, Exceptions. try catch are your best friends. First, you will be able to solve any errors caused by exception, and even use those errors to run other codes. For example, java doesn't have a way to detect whether a user input is an integer or not, unlike the javascript isNAN() function.
so you basically can try to parse that string as an integer/double. If it doesn't contain any alphabets or u can use it to process something. If it doesnt' a catch numberformat exception can allow you to do other stuff with the string. Very useful if you need to access PreparedStatements when workin with databases.