Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java homework Question 1: Write Overloaded Methods Write two overloaded methods

ID: 3670548 • Letter: J

Question

Java homework

Question 1: Write Overloaded Methods

Write two overloaded methods that sum up elements in an integer array. One method sums all elements in the array. The other method sums elements starting at a specified start and end position.

Question 2: The "this" Keyword

In your own words, explain the two uses of the "this" keyword.

Question 3: Static Variables and Methods

In your own words, explain:

: the difference between instance data variables and static variables

when it makes sense to use a static variable and method

the rules for what static methods can and cannot access

Question 4: Write a static Method

Write a static method to go inside the AudioItem class. The method should take in two AudioItem objects and return whether or not they have the same price.

Question 5: Aggregation

Use the files provided in the lecture to write an AudioStoreAccount class. This class describes an account you would create to buy things from an AudioStore.

An AudioStoreAccount class is described by an account ID a list of audio items owned.

Write the instance data, constructors, getters/setters, and a toString method.

Write a method to add a new AudioItem to the list of items owned.

Write a method to retrieve the number of AudioItems owned by the account holder.

Question 6: enum

Write an enumerated data type to represent the types of beverages sold at market: milk, soda, and beer.

Add instance data to represent the tax rate for each beverage: milk is 0%, soda is 5%, and beer is 10%.

Add a method to retrieve the tax associated with that type of beverage.

Explanation / Answer

Question 1: Write Overloaded Methods


public class ExamplePro {

void sumarray(int[] a)
{
int sum=0;
for(int i=0;i<a.length;i++)
{
sum=sum+a[i];
}
System.out.println("Total Sum"+sum);
}
void sumarray(int[] a,int k,int l)
{
int sum=0;
for(int i=k;i<=l;i++)
{
sum=sum+a[i];
}
System.out.println("Sum from "+k+"th"+"Element to "+l+"th element"+sum);
}
public static void main(String args[])
{
ExamplePro exe= new ExamplePro();
int a[]={1,2,3,4,5,6,7,8,9,10};
exe.sumarray(a);
exe.sumarray(a,1,5);
}
}

Question 2: The "this" Keyword

*this keyword is used to represent the same class.

*this keyword is used in the constructors to access any local members or methods of the class.

Question 3: Static Variables and Methods

Static variables can be accessed through out the object with out using the instance(object).

where as local variables can be accessed only through the object creation.

syntax for the static variable

Static method:-In the same way as static variable static method can be accessed with out object.

Ex: public static void main(String args[]) can be accessed outside to execute prograams without creating object.

Question 4: Write a static Method

public class Audioitem {
int price;
static void audiomethod(Audioitem a,Audioitem b)
{
if(a.price==b.price)
System.out.println("Both are having same price");
else
System.out.println("both are having different prices");
}
public static void main(String args[])
{
Audioitem a=new Audioitem();
Audioitem b=new Audioitem();
a.price=10;
b.price=10;
audiomethod(a,b);
}
}

Question 5: Aggregation

audio script said in the question not provided.

public class AudioStoreAccount {
int acctid;
String[] items=new String[100];
AudioStoreAccount(int actid,String[] item)
{
this.acctid=actid;
System.arraycopy(item, 0, this.items, 0, item.length);
}
void addItem(String item)
{
int len=this.items.toString().length();
items[len]=item;
}
void ownedItems()
{
for(int i=0;i<this.items.toString().length();i++)
System.out.println(items[i]);
}
public static void main(String args[])
{
String items[]={"tape","casette","pendrive","disk"};
AudioStoreAccount obj=new AudioStoreAccount(12,items);
obj.addItem("recent item rack");
obj.ownedItems();
}
}

Question 6: enum

enum Item {
milk(0),soda(5),beer(10);
private int price;
Item(int p) {
price = p;
}
int getTax() {
return price;
}
}
public class enumExe {
public static void main(String args[]){
System.out.println("All items taxes");
for (Item c : Item.values())
System.out.println(c + " Taxes "
+ c.getTax() + "% .");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote