Академический Документы
Профессиональный Документы
Культура Документы
Note: Please attempt all questions. Submit your assignment via Moodle. Late submissions would not
be accepted. You must work on the questions on your own, copying from others could result in a zero.
Make assumptions where necessary and mention them in your answer.
Q.1) Write a class named Cube that accepts two parameters in constructor low and high. The class has a private
method called cubeProduct that returns the sum of the cube of numbers between the low and
high. In other words, find sum of cubes of all numbers in range that is, if low is 3 and high is 10 then
it should return sum of 33 + 43 + 53 + 63 + _ _ _ + 103. [CLO-1].?
CUBE-CLASS:
package Question1;
public class Cube {
private int L,H;
public Cube(int low,int high) {
L = low;
H = high;
System.out.print("The sum of Cube of all the numbers is: "+cubeProduct(L,H));
}
private int cubeProduct (int L, int H) {
int sum = 0;
for (int i=L;i<=H;i++) {
sum += Math.pow(i,3);
}
return sum;
}
}
MAIN-CLASS:
package Question1;
import java.util.Scanner;
public class Question1_Main {
public static void main (String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter lower number: ");
int L = scanner.nextInt();
System.out.print("Enter higher number: ");
int H = scanner.nextInt();
scanner.close();
Cube call = new Cube (L,H);
}
}
Q.2) Write a program which removes space/spaces from a sentence (Note: Don’t use the split() and replace()
function, create your own logic to remove spaces by using indexof()). For example, input:
“Lorem ipsum dolor sit amet”, output should be: “Loremipsumdolorsitamet”.
package Question2;
import java.util.Scanner;
public class Question2_Main {
public static void main (String args[]) {
String space = " ";
System.out.println("Enter any Sentence:");
Scanner scanner = new Scanner(System.in);
String sentence = scanner.nextLine();
scanner.close();
for (int i=0;i<sentence.length();i++) {
String newSentence = sentence.substring(0,
sentence.indexOf(space));
sentence = sentence.substring(sentence.indexOf(space)+1,
sentence.length());
System.out.print(newSentence);
}
System.out.print(sentence);
}
}
Q.3) At the end of movie credits you see the year movies are produced in Roman numerals, for example, MCMXCV
II for 1997. To help the production staff determine the correct Roman numeral for the
production year, write a program that reads a year and displays the year in Roman numerals.
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
Remember that certain numbers are expressed by using a “subtraction”, that is when subsequent
number is greater than previous number then we subtract them. For example, IV : 5 1 = 4,
CD : 500 100 = 400, and so forth. Therefore:
MCMXCV II : 1000 + (1000 100) + (100 10) + 5 + 1 + 1 = 1997
You should use two classes RomanNumbers to receive roman numbers string in constructor and a
public method to return the integer representation. Use another class Process to get input from users
and pass it to RomanNumber Class, The class process is the main class as well [CLO-3].?
RomanNumbers-Class
package Question3;
public class RomanNumbers {
public RomanNumbers (String str) {
System.out.println("\nRoman Number: "+str);
System.out.println("Equivalent Integer: "+settingValues(str)+"\n");
}
public int settingValues(String str) {
int result = 0;
int len = str.length();
str = str + " ";
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
char next_char = str.charAt(i+1);
if (ch == 'M') {
result += 1000;
} else if (ch == 'C') {
if (next_char == 'M') {
result += 900;
i++;
} else if (next_char == 'D') {
result += 400;
i++;
} else {
result += 100;
}
} else if (ch == 'D') {
result += 500;
} else if (ch == 'X') {
if (next_char == 'C') {
result += 90;
i++;
} else if (next_char == 'L') {
result += 40;
i++;
} else {
result += 10;
}
} else if (ch == 'L') {
result += 50;
} else if (ch == 'I') {
if (next_char == 'X') {
result += 9;
i++;
} else if (next_char == 'V') {
result += 4;
i++;
} else {
result++;
}
} else { // if (ch == 'V')
result += 5;
}
}
return result;
}
}
ProcessMain-Class
package Question3;
import java.util.Scanner;
public class Process {
public static void main(String[] args) {
System.out.println("Enter any Roman Numeral to convert to Years (IVXLCDM)");
Scanner input = new Scanner(System.in);
String str = input.nextLine();
input.close();
RomanNumbers call = new RomanNumbers(str);
}
}
Good Luck
End of Assignment