Name : Moinuddin Md.
Tareq
ID ; 1621759042
Course : CSE215.9
Answer to the problem 2.6
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/[Link] to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/[Link] to edit this template
*/
package sumdigits;
/**
*
* @author minion
*/
import [Link];
public class SumDigits {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter a number between 0 and 1000: ");
int number = [Link]();
if (number < 0 || number > 1000) {
[Link]("Please enter a number between 0 and 1000.");
} else {
int sum = 0;
int originalNumber = number;
while (number > 0) {
sum += number % 10;
number /= 10;
}
[Link]("The sum of the digits in " + originalNumber + " is " + sum);
}
[Link]();
}
}
Answer to the problem 3.23
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/[Link] to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/[Link] to edit this template
*/
package pointinrectangle;
/**
*
* @author minion
*/
import [Link];
public class PointInRectangle {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter a point with two coordinates: ");
double x = [Link]();
double y = [Link]();
final double rectangleWidth = 10;
final double rectangleHeight = 5;
if ([Link](x) <= rectangleWidth / 2 && [Link](y) <= rectangleHeight / 2) {
[Link]("Point (" + x + ", " + y + ") is in the rectangle");
} else {
[Link]("Point (" + x + ", " + y + ") is not in the rectangle");
}
[Link]();
}
}
Answer to the problem 3.26
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/[Link] to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/[Link] to edit this template
*/
package divisiblebyfiveandsix;
/**
*
* @author minion
*/
import [Link];
public class DivisibleByFiveAndSix {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter an integer: ");
int number = [Link]();
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);
[Link]("Is " + number + " divisible by 5 and 6? " + divisibleByFiveAndSix);
[Link]("Is " + number + " divisible by 5 or 6? " + divisibleByFiveOrSix);
[Link]("Is " + number + " divisible by 5 or 6, but not both? " +
divisibleByFiveOrSixButNotBoth);
[Link]();
}
}
Answer to the problem 4.18
/**
*
* @author minion
*/
import [Link];
public class StudentMajorStatus {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner([Link]);
[Link]("Enter two characters: ");
String inputString = [Link]();
char major = [Link](0);
char status = [Link](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 ([Link]("Invalid Major") || [Link]("Invalid Status")) {
[Link]("Invalid input");
} else {
[Link](majorName + " " + statusName);
}
[Link]();
}
}
Answer to the problem 4.22
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/[Link] to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/[Link] to edit this template
*/
package substringcheck;
/**
*
* @author minion
*/
import [Link];
public class SubstringCheck {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner([Link]);
[Link]("Enter string s1: ");
String s1 = [Link]();
[Link]("Enter string s2: ");
String s2 = [Link]();
if ([Link](s2)) {
[Link](s2 + " is a substring of " + s1);
} else {
[Link](s2 + " is not a substring of " + s1);
}
[Link]();
}
}
Answer to the problem 9.7
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/[Link] to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/[Link] to edit this template
*/
package account;
/**
*
* @author minion
*/
import [Link];
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) {
[Link] = id;
[Link] = balance;
annualInterestRate = 0;
dateCreated = new Date();
}
public int getId() {
return id;
}
public void setId(int id) {
[Link] = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
[Link] = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
[Link] = 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);
[Link](4.5);
[Link](2500);
[Link](3000);
[Link]("Balance: $" + [Link]());
[Link]("Monthly Interest: $" + [Link]());
[Link]("Date Created: " + [Link]());
}
Answer to the problem 9.8
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/[Link] to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/[Link] 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) {
[Link] = speed;
}
public boolean getOn() {
return on;
}
public void setOn(boolean on) {
[Link] = on;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
[Link] = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
[Link] = 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();
[Link](FAST);
[Link](10);
[Link]("yellow");
[Link](true);
Fan fan2 = new Fan();
[Link](MEDIUM);
[Link](5);
[Link]("blue");
[Link](false);
[Link]([Link]());
[Link]([Link]());