1.tushar Assignment 1
1.tushar Assignment 1
int i = 10;
int n = i++%5;
Answer:i =11 and n = 0.
b. What are the final values of i and n if instead of using the postfix increment operator (i++), you
use the prefix version (++i))?
Answer:i =11 and n = 1.
Answer:"If someCondition is true , assign the value of value1 to result . Otherwise, assign the value of
value2 to result ."
Expression
Answer: expressions.
1 * 2 * 3
5. Statements are roughly equivalent to sentences in natural languages, but instead of ending with a
period, a statement ends with a __"semicolon"_.
6. A block is a group of zero or more statements between balanced _"braces"__ and can be used
anywhere a single statement is allowed.
Exercises
Identify the following kinds of expression statements:
● aValue = 8933.234;
Answer:assignment statement
● aValue++;
● System.out.println("Hello World!");
Questions :
4. Hiding internal data from the outside world, and accessing it only through publicly exposed methods is
known as data "encapsulation".
6. Common behavior can be defined in a "superclass" and inherited into a "subclass" using the "extends"
keyword.
Argument int n
Answer:package Tushar;
public class calculate_sum {
// Function to calculate the sum
// of numbers divisible by 3 or 5
static int sum(int N, int X, int Y)
{
int S1, S2, S3;
S1 = ((N / X)) * (2 * X + (N / X - 1) * X)
/ 2;
S2 = ((N / Y)) * (2 * Y + (N / Y - 1) *
Y) / 2;
S3 = ((N / (X * Y))) * (2 * (X * Y)
+ (N / (X * Y) - 1) * (X * Y))/
2;
return S1 + S2 - S3;
}
public static void main(String
[]args)
{
int N = 10;
int X = 3, Y = 5;
System.out.println(sum(N, X, Y));
}
}
Output is: 33
Exercise 6: Create a class with a method to find the difference between the sum of the squares and the
square of the sum of the first n natural numbers.
Argument int n
(1^2+2^2+3^2+….9^2+10^2)-
(1+2+3+4+5…+9+10)^2
Answer:
package Tushar;
public class difference {
static int square_diff(int n ) {
int l,k, m;
l = (n * (n + 1) * (2 * n + 1)) / 6;
k = (n * (n + 1)) / 2;
k = k * k;
m =
Math.abs(l - k);
return m;
}
public static void main(String s[])
{
int n = 10;
System.out.println(square_diff(n)); }
}
Output:2640
package Tushar;
Answer:
import java.util.Scanner;
public class increasing {
public static void main(String
args[]) {
int num;
boolean flag = false;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number : ");
num = scanner.nextInt();
int currentDigit = num % 10;
num = num/10;
while(num>0){
if(currentDigit <= num % 10){
flag = true;
break;
}
currentDigit = num % 10;
num = num/10;
}
if(flag){
System.out.println("Digits are not in
increasing order.");
}else{
System.out.println("Digits are in
increasing order.");
}
} }
Exercise 8: Create a method to check if a number is a power of two or not
Method Name checkNumber
Argument int n
Ex: 8 is a power of 2
Answer:package Tushar;
public class check {
static boolean check_power(int n ) {
while(n%2==0)
{
n=n/2;
}
if(n==1)
{
return true;
}
else
{
return false;
}}
public static void main(String s[])
{
int n = 8;
System.out.println(check_power(n)); }
}
Output: “true”