Need help with a few programming exercises. The language being used for all of them is JAVA.
1) exercises - branching (language is JAVA)
• Write a method whatToWear(int temp) that takes a temperature and then outputs a string for what to wear in different weather conditions. There must be at least 3 categories. For example, whatToWear(90) might return “shorts” and whatToWear(30) might return “down coat”
2) Enum exercise
• Define an enum Seasons, and rewrite the whatToWear method to use enums and switch statement
3) Loops exercise
• Expected cost
• Each year the expected cost of an item increases by the amount of inflation. For example
• expectedCostNextYear = cost*(1+inflation) • where inflation might by .04, representing 4%.
• Write a method which takes inputs: cost, inflation, years and returns expected cost in n years.
4) Loops exercise
Rewrite the previous code using a while loop (number 3)
5) Nested Loop exercise
• Write a program to create the pattern, shown below for n=5, for any n
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
6) for-each exercise
• Create an array of words • Use a for-each loop to print out the number of characters in each word
7) Partially Filled Array exercise
• Write a program that allocates an array of 50 strings.
• Ask the user to input city names to store in the array until the user enters “quit”.
8) 2D array exercise
1. write a method columnSum(int[][] a, int col) that returns the sum of the values of column col
2. Write a method public static boolean isSquare(int[][] a) that checks if the array is square (i.e. every row has the same length as a itself)
9) Classes Exercise
• Consider a class Student •
Think of four instance variables
• Write four possible constructors
10) Classes exercise
• Define a class BlogEntry that has variables
• username
• text
• date
• Methods
• getters/setters
• displayEntry()
• getSummary() // return first 10 words
Thanks in advance for your help!
3)
// InflationAmount.java
public class InflationAmount {
public static void main(String[] args) {
double
cost=5000,inflation=0.04;
int years=10;
double expectedCost=calExpectedCost(cost,inflation,years);
System.out.println("Expected Cost
:$"+expectedCost);
}
private static double
calExpectedCost(double cost, double inflation, int years) {
double expectedCostNextYear =
0;
for(int i=0;i<years;i++)
{
expectedCostNextYear =
cost*(1+inflation);
cost=expectedCostNextYear;
}
return expectedCostNextYear;
}
}
________________________
Output:
Expected Cost :$7401.221424591723
________________________
4)
public class InflationAmount {
public static void main(String[] args) {
double
cost=5000,inflation=0.04;
int years=10;
double expectedCost=calExpectedCost(cost,inflation,years);
System.out.println("Expected Cost :$"+expectedCost);
}
private static double calExpectedCost(double cost,
double inflation, int years) {
double expectedCostNextYear =
0;
int i=0;
while(i<years)
{
expectedCostNextYear = cost*(1+inflation);
cost=expectedCostNextYear;
i++;
}
return expectedCostNextYear;
}
}
_______________________________
Output:
Expected Cost
:$7401.221424591723
_______________________________
5)
// LoopPattern.java
import java.util.Scanner;
public class LoopPattern {
public static void main(String[] args) {
int n;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
System.out.print("Enter n:");
n=sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
_____________________________
Output:
Enter n:5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
______________________________
6)
// WordLength.java
public class WordLength {
public static void main(String[] args) {
String
words[]={"Hello","Wonderful","Maria","Great","Message","Monitor"};
for(int i=0;i<words.length;i++)
{
System.out.println("No of Characters in
\""+words[i]+"\" :"+words[i].length());
}
}
}
_______________________
Output:
No of Characters in "Hello" :5
No of Characters in "Wonderful" :9
No of Characters in "Maria" :5
No of Characters in "Great" :5
No of Characters in "Message" :7
No of Characters in "Monitor" :7
_______________________
7)
// PartiallyFilledArray.java
import java.util.Scanner;
public class PartiallyFilledArray {
public static void main(String[] args) {
final int MAX=50;
String words[]=new
String[MAX];
int cnt=0;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
System.out.print("Enter word (quit
to exit) :");
String word=sc.next();
while(!word.equalsIgnoreCase("quit"))
{
words[cnt]=word;
cnt++;
System.out.print("Enter word (quit to exit) :");
word=sc.next();
}
System.out.println("\nDisplaying
the words :");
for(int i=0;i<cnt;i++)
{
System.out.println(words[i]);
}
}
}
__________________________
Output:
Enter word (quit to exit) :hello
Enter word (quit to exit) :sachin
Enter word (quit to exit) :great
Enter word (quit to exit) :beautiful
Enter word (quit to exit) :sara
Enter word (quit to exit) :quit
Displaying the words :
hello
sachin
great
beautiful
sara
_______________________Thank You
Need help with a few programming exercises. The language being used for all of them is...
Need help with a few questions! Using the JAVA language please. 1) exercises - branching Write a method whatToWear(int temp) that takes a temperature and then outputs a string for what to wear in different weather conditions. There must be at least 3 categories. For example, whatToWear(90) might return “shorts” and whatToWear(30) might return “down coat” 2) Enum exercise • Define an enum Seasons, and rewrite the whatToWear method to use enums and switch statement 8) 2D array exercise 1....
Loops | Methods | Arrays(programming language java) in software (ATOM) Write a toolkit that includes some common functions a user might want or need An encryption/ decryption method. Both methods should take in a String (the String that needs to be changed), and an encryption value. The methods need to change the String by the value Write a method that takes an integer array as an argument. The method should return the sum of all of the elements Write a...
In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...
Java Programming Assignment
Write a class named 2DArrayOperations with the following static
methods:
getTotal . This method should accept a
two-dimensional array as its argument and return the total of all
the values in the array. Write overloaded versions of this method
that work with int , double , and long arrays.
(A)
getAverage . This method should accept a
two-dimensional array as its argument and return the average of all
the values in the array. Write overloaded versions of...
Programming Language : JAVA Write a class named Ship. Its purpose is to model a ship in the BattleShip game and its placement on the Battleship board. Ships exist on a 10 x 10 battleship board with ten rows labeled A through J and columns labeled 1 through 9 and the final column labeled 0 to indicate the tenth column. We will refer to the Ship placed on the board at the origin which the pair (r,c) where in the...
Has to be in Java programming language
Java Python mirrorLength We'll say that a "mirror" section in an array is a group of contiguous elements such that somewhere in the array, the same group appears in reverse order. For example, the largest mirror section in {1, 2, 3, 8, 9, 3, 2, 1} is length 3 (the {1, 2, 3} part). Return the size of the largest mirror section found in the given array. 3 mirrorLength([1, 2, 3, 8, 9,...
Hi I need some help on this lab. The world depends on its successfull compilation. /* Lab5.java Arrays, File input and methods Read the comments and insert your code where indicated. Do not add/modify any output statements */ import java.io.*; import java.util.*; public class Lab5 { public static void main (String[] args) throws Exception { final int ARRAY_MAX = 30; // "args" is the list of tokens you put after "java Project3" on command line if (args.length == 0 )...
This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...
Programming in java
In this part of this Lab exercise, you will need to create a new class named ArrayShiftMult. This class carries out simple manipulation of an array. Then you should add a main method to this class to check that your code does what it is supposed to do. 1. Create a BlueJ project named LAB06 as follows: a. Open the P drive and create a folder named Blue), if it does not exist already. Under this folder,...
Need help to answer this method in java Write a method int indexFirstOne(int[ ] input) The input array is sorted, and every element is 0 or 1. Return the index of the first 1. If there are no 1s in the array, return -1. The worst-case runtime must be O(logn)where n is the number of elements (no credits for slower runtimes) Example: a = [0,0,1,1,1] return 2 a = [ 0,0,0,1] return 3 a = [0,0,0] return -1 int indexFirstOne...