1.
//Assignment1.java
public class Assignment1 {
public static void main(String[] args) {
//integer variable
int intNumber=15;
//double variable
double doubleNumber=6;
//declare ratio of type double
double ratio;
//Divide the integer by integer yields an integer value
//even the intNumber=15 divided by 6, the result is 2
//since decimal value,0.5 is discarded from 2.5.
ratio=intNumber/6;
System.out.println(\"intNumber = \"+intNumber);
System.out.println(\"doubleNumber = \"+doubleNumber);
//print ratio value ,2 to console
System.out.println(\"ratio=intNumber/6 = \"+ratio);
//Re-assignment
//Divide the integer by double yields an double value
//2.5 is double value stored in a double variable, ratio
ratio=intNumber/doubleNumber;
//print ration to console
System.out.println(\"ratio=intNumber/doubleNumber=\"+ratio);
}
}//end of class Assignment1
-------------------------------------------------------------------------------------------------------------------
2.
//Assignment2.java
public class Assignment2 {
public static void main(String[] args) {
//since the ascii values of a =97
//and b=98,a+b=195 is printed
System.out.println(\'a\'+\'b\');
//double quotation is string
//value and converts the characters
//to string
System.out.println(\" \"+\'a\'+\'b\');
//even double quotation is a string
//the paranthesis separates the character
// values from the string and sum the
//a+b=195 to console
System.out.println(\" \"+(\'a\'+\'b\'));
}
}
-------------------------------------------------------------------------------------------------------------------
3.
//Assignment3.java
public class Assignment3 {
public static void main(String[] args) {
//integer variable
int intNumber=15;
//double variable
double doubleNumber=6;
//since the string \"These are numbers: \" will not sum the values
//intNumber and doubleNumber so they print as individual values along with string
System.out.println(\"These are numbers: \" + intNumber + doubleNumber);
//here intNumber and doubleNumber are separatead by paranthesis
//so they become added up and print the result to console.
System.out.println(\"These are numbers: \" + (intNumber + doubleNumber));
}
}
----------------------------------------------------------------------------------------------------------------
4.
//Assignment4.java
public class Assignment4 {
public static void main(String[] args) {
//integer variable
int intNumber=15;
//double variable
double doubleNumber=6;
// in the paranthesis, the multiplucation got prioority over / and +
//intNumber*doubleNumber=90.0, then 5/90=0.05, .05 is dicarded
//25+0+1=26 will be printed
System.out.println(\"Formula1: \" + (25 +5/intNumber*doubleNumber + 1));
//Preference of parenthesis from right to left
//(doubleNumber+1)=6+1=7
//(25+5)=30
// The division operator gets percedence from left to right
// then * got precedence
//30/15*(6+1)=30/15*7=2*7=14
System.out.println(((25+5)/intNumber*(doubleNumber + 1)));
}
}
Solution
1.
//Assignment1.java
public class Assignment1 {
pub.
1. Assignment1.java public class Assignment1 { public sta.pdf
1. 1.
//Assignment1.java
public class Assignment1 {
public static void main(String[] args) {
//integer variable
int intNumber=15;
//double variable
double doubleNumber=6;
//declare ratio of type double
double ratio;
//Divide the integer by integer yields an integer value
//even the intNumber=15 divided by 6, the result is 2
//since decimal value,0.5 is discarded from 2.5.
ratio=intNumber/6;
System.out.println("intNumber = "+intNumber);
System.out.println("doubleNumber = "+doubleNumber);
//print ratio value ,2 to console
System.out.println("ratio=intNumber/6 = "+ratio);
//Re-assignment
//Divide the integer by double yields an double value
//2.5 is double value stored in a double variable, ratio
ratio=intNumber/doubleNumber;
//print ration to console
System.out.println("ratio=intNumber/doubleNumber="+ratio);
}
}//end of class Assignment1
-------------------------------------------------------------------------------------------------------------------
2.
//Assignment2.java
public class Assignment2 {
public static void main(String[] args) {
//since the ascii values of a =97
2. //and b=98,a+b=195 is printed
System.out.println('a'+'b');
//double quotation is string
//value and converts the characters
//to string
System.out.println(" "+'a'+'b');
//even double quotation is a string
//the paranthesis separates the character
// values from the string and sum the
//a+b=195 to console
System.out.println(" "+('a'+'b'));
}
}
-------------------------------------------------------------------------------------------------------------------
3.
//Assignment3.java
public class Assignment3 {
public static void main(String[] args) {
//integer variable
int intNumber=15;
//double variable
double doubleNumber=6;
//since the string "These are numbers: " will not sum the values
//intNumber and doubleNumber so they print as individual values along with string
System.out.println("These are numbers: " + intNumber + doubleNumber);
//here intNumber and doubleNumber are separatead by paranthesis
//so they become added up and print the result to console.
System.out.println("These are numbers: " + (intNumber + doubleNumber));
}
}
----------------------------------------------------------------------------------------------------------------
4.
//Assignment4.java
public class Assignment4 {
3. public static void main(String[] args) {
//integer variable
int intNumber=15;
//double variable
double doubleNumber=6;
// in the paranthesis, the multiplucation got prioority over / and +
//intNumber*doubleNumber=90.0, then 5/90=0.05, .05 is dicarded
//25+0+1=26 will be printed
System.out.println("Formula1: " + (25 +5/intNumber*doubleNumber + 1));
//Preference of parenthesis from right to left
//(doubleNumber+1)=6+1=7
//(25+5)=30
// The division operator gets percedence from left to right
// then * got precedence
//30/15*(6+1)=30/15*7=2*7=14
System.out.println(((25+5)/intNumber*(doubleNumber + 1)));
}
}
Solution
1.
//Assignment1.java
public class Assignment1 {
public static void main(String[] args) {
//integer variable
int intNumber=15;
//double variable
double doubleNumber=6;
//declare ratio of type double
double ratio;
//Divide the integer by integer yields an integer value
//even the intNumber=15 divided by 6, the result is 2
//since decimal value,0.5 is discarded from 2.5.
4. ratio=intNumber/6;
System.out.println("intNumber = "+intNumber);
System.out.println("doubleNumber = "+doubleNumber);
//print ratio value ,2 to console
System.out.println("ratio=intNumber/6 = "+ratio);
//Re-assignment
//Divide the integer by double yields an double value
//2.5 is double value stored in a double variable, ratio
ratio=intNumber/doubleNumber;
//print ration to console
System.out.println("ratio=intNumber/doubleNumber="+ratio);
}
}//end of class Assignment1
-------------------------------------------------------------------------------------------------------------------
2.
//Assignment2.java
public class Assignment2 {
public static void main(String[] args) {
//since the ascii values of a =97
//and b=98,a+b=195 is printed
System.out.println('a'+'b');
//double quotation is string
//value and converts the characters
//to string
System.out.println(" "+'a'+'b');
//even double quotation is a string
//the paranthesis separates the character
// values from the string and sum the
//a+b=195 to console
System.out.println(" "+('a'+'b'));
}
}
-------------------------------------------------------------------------------------------------------------------
5. 3.
//Assignment3.java
public class Assignment3 {
public static void main(String[] args) {
//integer variable
int intNumber=15;
//double variable
double doubleNumber=6;
//since the string "These are numbers: " will not sum the values
//intNumber and doubleNumber so they print as individual values along with string
System.out.println("These are numbers: " + intNumber + doubleNumber);
//here intNumber and doubleNumber are separatead by paranthesis
//so they become added up and print the result to console.
System.out.println("These are numbers: " + (intNumber + doubleNumber));
}
}
----------------------------------------------------------------------------------------------------------------
4.
//Assignment4.java
public class Assignment4 {
public static void main(String[] args) {
//integer variable
int intNumber=15;
//double variable
double doubleNumber=6;
// in the paranthesis, the multiplucation got prioority over / and +
//intNumber*doubleNumber=90.0, then 5/90=0.05, .05 is dicarded
//25+0+1=26 will be printed
System.out.println("Formula1: " + (25 +5/intNumber*doubleNumber + 1));
//Preference of parenthesis from right to left
//(doubleNumber+1)=6+1=7
//(25+5)=30
// The division operator gets percedence from left to right
// then * got precedence