[go: up one dir, main page]

0% found this document useful (0 votes)
19 views5 pages

2

Uploaded by

foysal.rko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

2

Uploaded by

foysal.rko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2.

6-Sum the digits in an integer

import java.util.Scanner; public class SumDigits {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter a number between 0 and 1000: ");

int num = input.nextInt();

int sum = 0;

sum += num % 10;

num /= 10;

sum += num % 10;

num = num / 10;

sum += num % 10;

System.out.println("The sum of the digits is " + sum); } }

3.23-Point in a rectangle

import java.util.Scanner;

public class PointInRectangle {

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();

if (x <= 5 && x >= -5 && y <= 2.5 && y >= -2.5) {

System.out.println("Point (" + x + ", " + y + ") is in the rectangle"); }

else {

System.out.println("Point (" + x + ", " + y + ") is not in the rectangle"); } } }

3.26-Divisibility Check

import java.util.Scanner;
public class DivisibilityCheck {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter an integer: ");

int num = input.nextInt();

boolean divBy5And6 = (num % 5 == 0) && (num % 6 == 0);

boolean divBy5or6 = (num % 5 == 0) || (num % 6 == 0);

boolean divBy5or6NotBoth = divBy5or6 && !(divBy5And6);

System.out.println("Is " + num + " divisible by 5 and 6? " + divBy5And6);

System.out.println("Is " + num + " divisible by 5 or 6? " + divBy5or6);

System.out.println("Is " + num + " divisible by 5 or 6, but not both? " + divBy5or6NotBoth); } }

4.18-Student Major and Status

import java.util.Scanner;

public class StudentMajorStatus {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter two characters: ");

String majorStatus = input.next();

char major = majorStatus.charAt(0);

char status = majorStatus.charAt(1);

String majorString = "";

if (major == 'M') majorString = "Mathematics";

else if (major == 'C') majorString = "Computer Science";

else if (major == 'I') majorString = "Information Technology";

else { System.out.println("Invalid input");

return; }

String statusString = "";

if (status == '1') statusString = "Freshman";


else if (status == '2') statusString = "Sophomore";

else if (status == '3') statusString = "Junior";

else if (status == '4') statusString = "Senior";

else { System.out.println("Invalid input");

return; }

System.out.println(majorString + " " + statusString); } }

4.22-Check substring

import java.util.Scanner;

public class CheckSubstring {

public static void main(String[] args) {

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); } } }

9.7-The account class

import java.util.Date;

public class Account {

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; }

public double getMonthlyInterest() {

return balance * getMonthlyInterestRate();}


public void withdraw(double amount) {

if (amount > balance) {

System.out.println("Insufficient balance."); }

else { balance -= amount; } }

public void deposit(double amount) { balance += amount; }

public static void main(String[] args) {

Account acc = new Account(1122, 20000);

acc.setAnnualInterestRate(4.5);

acc.withdraw(2500);

acc.deposit(3000);

System.out.println("Balance: " + acc.getBalance());

System.out.println("Monthly interest: " + acc.getMonthlyInterest());

System.out.println("Date created: " + acc.getDateCreated()); } }

9.8-the Fan class

public class Fan { 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 isOn() { 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; } public String toString() { if (on) { return "Fan speed: " + speed + ", color: " + color + ", radius: " +
radius; } else { return "Fan is off, color: " + color + ", radius: " + radius; } } public static void main(String[]
args) { 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()); } }

You might also like