[go: up one dir, main page]

0% found this document useful (0 votes)
113 views7 pages

Assignment 5 & 6

The document describes 4 Java assignments: 1. Create a package mypack with classes Balance and AcctBalance, where Balance has member variables and methods and AcctBalance instantiates a Balance object. 2. Write a program to demonstrate usage of access modifiers (public, private, protected, default) in the same package, subclass, and different class. 3. Create a package packCal with a Calculator class that imports classes from packages packAdd, packMulti, packSub, packDiv to perform addition, multiplication, subtraction, and division. 4. Create a package with methods to reverse a string and count the alphabets in a string.

Uploaded by

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

Assignment 5 & 6

The document describes 4 Java assignments: 1. Create a package mypack with classes Balance and AcctBalance, where Balance has member variables and methods and AcctBalance instantiates a Balance object. 2. Write a program to demonstrate usage of access modifiers (public, private, protected, default) in the same package, subclass, and different class. 3. Create a package packCal with a Calculator class that imports classes from packages packAdd, packMulti, packSub, packDiv to perform addition, multiplication, subtraction, and division. 4. Create a package with methods to reverse a string and count the alphabets in a string.

Uploaded by

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

Name – Tushar Sonker

Roll No. 2003630100163


Sec- C

Java Assignment 5 & 6


1.create a package mypack which contains the class Balance that has data

member name and bal,a constructor and a method show().mypack contains

other class AcctBalance whose main method instantiate the Balance class.

Ans –
mypack/Balance.java—

package mypack;

public class Balance {


String name;
double bal;

// Constructor
public Balance(String name, double bal) {
this.name = name;
this.bal = bal;
}

// Method to show balance


public void show() {
System.out.println("Name: " + name);
System.out.println("Balance: $" + bal);
}
}
AcctBalance.java
import mypack.Balance;

public class AcctBalance {

public static void main(String[] args) {

// Instantiate the Balance class

Balance account = new Balance("John Doe", 1000.0);

// Call the show() method to display account information

account.show();

2.write a program for use of all access modifiers default,public,private,

protected in same package,subclass in the same package,different class in same package.

Ans –

AccessModifiersDemo.java

package mypackage; // Package name

public class AccessModifiersDemo {

public static void main(String[] args) {

// Create an instance of MyClass

MyClass myClass = new MyClass();

// Access members using different access modifiers

myClass.publicMethod(); // Accessing a public method

myClass.defaultMethod(); // Accessing a default method

// myClass.privateMethod(); // Private method cannot be accessed from outside the class

myClass.protectedMethod(); // Accessing a protected method

}
MyClass.java (Class in the same package)

package mypackage; // Package name

class MyClass {

public void publicMethod() {

System.out.println("This is a public method.");

void defaultMethod() {

System.out.println("This is a default (package-private) method.");

private void privateMethod() {

System.out.println("This is a private method.");

protected void protectedMethod() {

System.out.println("This is a protected method.");

class Subclass extends MyClass {

public void demonstrateAccessModifiers() {

// Access members from the superclass using different access modifiers

publicMethod();

defaultMethod();

privateMethod();

protectedMethod();

}
3.create a package packCal in which main class Calculater and you create 4 different
class,add,Multi,

sub,Div in packAdd,packMulti,packSub,packDiv.These classes can be accessed through Calculator .

Ans-

Add.java (inside packAdd)

package packAdd;

public class Add {

public static int add(int a, int b) {

return a + b;

Multi.java (inside packMulti)

package packMulti;

public class Multi {

public static int multiply(int a, int b) {

return a * b;

Sub.java (inside packSub)

package packSub;

public class Sub {

public static int subtract(int a, int b) {

return a - b;

}
}

Div.java (inside packDiv)

package packDiv;

public class Div {

public static double divide(double a, double b) {

if (b != 0) {

return a / b;

} else {

System.err.println("Division by zero is not allowed.");

return Double.NaN;

Calculator.java (inside packCal)

package packCal;

import packAdd.Add;

import packMulti.Multi;

import packSub.Sub;

import packDiv.Div;

public class Calculator {

public static void main(String[] args) {

int num1 = 10;

int num2 = 5;

// Use classes from different packages

int sum = Add.add(num1, num2);


int product = Multi.multiply(num1, num2);

int difference = Sub.subtract(num1, num2);

double division = Div.divide(num1, num2);

System.out.println("Sum: " + sum);

System.out.println("Product: " + product);

System.out.println("Difference: " + difference);

System.out.println("Division: " + division);

4.To create package which has two methods

a) To reverse the given string

(b) to give the number of alphabets in a string.

StringUtil.java (inside the package)

package stringutilities;

public class StringUtil {

// Method to reverse a given string

public static String reverseString(String input) {

StringBuilder reversed = new StringBuilder(input);

return reversed.reverse().toString();

public static int countAlphabets(String input) {

int alphabetCount = 0;

for (char ch : input.toCharArray()) {

if (Character.isLetter(ch)) {

alphabetCount++;

}
return alphabetCount;

MainApplication.java

import stringutilities.StringUtil;

public class MainApplication {

public static void main(String[] args) {

String inputString = "Hello, World! 123";

// Reverse the given string

String reversedString = StringUtil.reverseString(inputString);

System.out.println("Reversed String: " + reversedString);

// Count the number of alphabets in the string

int alphabetCount = StringUtil.countAlphabets(inputString);

System.out.println("Alphabet Count: " + alphabetCount);

You might also like