In: Computer Science
Where the OOD approach is specified use a separate test class to create your objects.
Kindly upvote if
this helped
As there are multiple questions answering initial 2 Questons.
Kindly create seperate questions for multiple questions.
1)
Odd.java
import java.util.Scanner;
public class Odd {
public void OddTest() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int input = sc.nextInt();
sc.close();
if( input%2!=0 && input>0 ) { // number is odd and positive
System.out.println("Number is an odd positive number");
}else {
System.out.println("Number is either even or negative");
}
}
}
OddTest.java
class OddTest{
public static void main(String[] args) {
Odd d = new Odd();
d.OddTest();
}
}
2.)
Guessing.java
import java.util.ArrayList;
import java.util.Scanner;
public class Guessing{
public int getNumber() {
return number;
}
int number = 10;
public void GuessNumber() {
ArrayList<Integer> numbers = new ArrayList<Integer>(); // arrayList to store the guesses
Scanner sc = new Scanner(System.in);
int num;
System.out.println("Guess the number");
for(int i=1;i<=5;i++) {
num = Integer.parseInt(sc.nextLine());
numbers.add(num);
}
sc.close();
boolean flag = true;
for(int i=0;i<5;i++) {
if(numbers.get(i) == getNumber()) {
int t = i+1;
System.out.println("Guess number "+t+" matched");
flag = false;
}
}
if(flag) {
System.out.println("No guess successful");
}
}
}
GuessingTest.java
class GuessingTest{
public static void main(String[] args) {
Guessing g = new Guessing();
g.GuessNumber();
}
}
Snapshot for indent
Adding solution to other parts for student
understanding
class Tax{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter salary of 2 members");
double s1 = Double.parseDouble(sc.nextLine());
double s2 = Double.parseDouble(sc.nextLine());
double income = s1+s2;
double tax = 0;
sc.close();
if(income>=15000 && income < 20000) {
tax = income * 0.1; //10%
}
if(income>=20000 && income < 30000) {
tax = income * 0.2; //20%
}
if(income >=30000) {
tax = income * 0.3; //30%
}
System.out.println("Tax is "+tax);
}
}
class Inte{
public static int greaterThan(int a , int b) {
if(a>b) {
return 1;
}
return 0;
}
public static void main(String[] args) {
greaterThan(10, 20);
}
}
class Initials{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 initials");
String a = sc.nextLine();
String b = sc.nextLine();
if(a.compareTo(b)>0) {
System.out.println(a+ " falls after "+b);
}else if(a.compareTo(b)<0) {
System.out.println(a+ " falls before "+b);
}else {
System.out.println("Both are equal");
sc.close();
}
}
}