Name : Moinuddin Md.
Tareq
ID ; 1621759042
Course : CSE215.9
Answer to the problem 2.6
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package sumdigits;
/**
*
* @author minion
*/
import java.util.Scanner;
public class SumDigits {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number between 0 and 1000: ");
int number = input.nextInt();
if (number < 0 || number > 1000) {
System.out.println("Please enter a number between 0 and 1000.");
} else {
int sum = 0;
int originalNumber = number;
while (number > 0) {
sum += number % 10;
number /= 10;
}
System.out.println("The sum of the digits in " + originalNumber + " is " + sum);
}
input.close();
}
}
Answer to the problem 3.23
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package pointinrectangle;
/**
*
* @author minion
*/
import java.util.Scanner;
public class PointInRectangle {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a point with two coordinates: ");
double x = input.nextDouble();
double y = input.nextDouble();
final double rectangleWidth = 10;
final double rectangleHeight = 5;
if (Math.abs(x) <= rectangleWidth / 2 && Math.abs(y) <= rectangleHeight / 2) {
System.out.println("Point (" + x + ", " + y + ") is in the rectangle");
} else {
System.out.println("Point (" + x + ", " + y + ") is not in the rectangle");
}
input.close();
}
}
Answer to the problem 3.26
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package divisiblebyfiveandsix;
/**
*
* @author minion
*/
import java.util.Scanner;
public class DivisibleByFiveAndSix {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
boolean divisibleByFiveAndSix = number % 5 == 0 && number % 6 == 0;
boolean divisibleByFiveOrSix = number % 5 == 0 || number % 6 == 0;
boolean divisibleByFiveOrSixButNotBoth = (number % 5 == 0 && number % 6 != 0) ||
(number % 5 != 0 && number % 6 == 0);
System.out.println("Is " + number + " divisible by 5 and 6? " + divisibleByFiveAndSix);
System.out.println("Is " + number + " divisible by 5 or 6? " + divisibleByFiveOrSix);
System.out.println("Is " + number + " divisible by 5 or 6, but not both? " +
divisibleByFiveOrSixButNotBoth);
input.close();
}
}
Answer to the problem 4.18
/**
*
* @author minion
*/
import java.util.Scanner;
public class StudentMajorStatus {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.print("Enter two characters: ");
String inputString = input.nextLine();
char major = inputString.charAt(0);
char status = inputString.charAt(1);
String majorName = "";
String statusName = "";
switch (major) {
case 'M':
majorName = "Mathematics";
break;
case 'C':
majorName = "Computer Science";
break;
case 'I':
majorName = "Information Technology";
break;
default:
majorName = "Invalid Major";
break;
}
switch (status) {
case '1':
statusName = "Freshman";
break;
case '2':
statusName = "Sophomore";
break;
case '3':
statusName = "Junior";
break;
case '4':
statusName = "Senior";
break;
default:
statusName = "Invalid Status";
break;
}
if (majorName.equals("Invalid Major") || statusName.equals("Invalid Status")) {
System.out.println("Invalid input");
} else {
System.out.println(majorName + " " + statusName);
}
input.close();
}
}
Answer to the problem 4.22
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package substringcheck;
/**
*
* @author minion
*/
import java.util.Scanner;
public class SubstringCheck {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.print("Enter string s1: ");
String s1 = input.nextLine();
System.out.print("Enter string s2: ");
String s2 = input.nextLine();
if (s1.contains(s2)) {
System.out.println(s2 + " is a substring of " + s1);
} else {
System.out.println(s2 + " is not a substring of " + s1);
}
input.close();
}
}
Answer to the problem 9.7
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package account;
/**
*
* @author minion
*/
import java.util.Date;
public class Account {
/**
* @param args the command line arguments
*/
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
public Account() {
id = 0;
balance = 0;
annualInterestRate = 0;
dateCreated = new Date();
}
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
annualInterestRate = 0;
dateCreated = new Date();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public double getMonthlyInterestRate() {
return annualInterestRate / 12 / 100;
}
public double getMonthlyInterest() {
return balance * getMonthlyInterestRate();
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
public static void main(String[] args) {
// TODO code application logic here
Account account = new Account(1122, 20000);
account.setAnnualInterestRate(4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance: $" + account.getBalance());
System.out.println("Monthly Interest: $" + account.getMonthlyInterest());
System.out.println("Date Created: " + account.getDateCreated());
}
Answer to the problem 9.8
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package fan;
/**
*
* @author minion
*/
public class Fan {
/**
* @param args the command line arguments
*/
public static final int SLOW = 1;
public static final int MEDIUM = 2;
public static final int FAST = 3;
private int speed;
private boolean on;
private double radius;
private String color;
public Fan() {
speed = SLOW;
on = false;
radius = 5;
color = "blue";
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean getOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
if (on) {
return "Fan speed: " + speed + ", color: " + color + ", radius: " + radius;
} else {
return "Fan color: " + color + ", radius: " + radius + ", fan is off";
}
}
public static void main(String[] args) {
// TODO code application logic here
Fan fan1 = new Fan();
fan1.setSpeed(FAST);
fan1.setRadius(10);
fan1.setColor("yellow");
fan1.setOn(true);
Fan fan2 = new Fan();
fan2.setSpeed(MEDIUM);
fan2.setRadius(5);
fan2.setColor("blue");
fan2.setOn(false);
System.out.println(fan1.toString());
System.out.println(fan2.toString());