Compute Recursively Total Number Of Blocks In Triangle With Given Number Of Rows
Jul 8, 2014
We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows.
triangle(0) → 0
triangle(1) → 1
triangle(2) → 3
View Replies
ADVERTISEMENT
Feb 20, 2014
In a forest, there are some bamboo trees .The length of each tree get doubled during winter and increases by one unit in summer , write a Java program to calculate the total length of n number of bamboo trees in M number of seasons. The season always starts with winter.
import java.util.Scanner;
public class Tree {
public static void main(String args[]) {
int length;
int season;
[Code] ....
View Replies
View Related
Mar 28, 2014
I cannot get the right output for my program. I need to get the total number of pennies in dollar amount for the output of Total Pay. This is my program. The directions are:
Write a program that calculates the amount a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies. Do not accept a number less than 1 for the number of days worked.
import java.util.Scanner;
import java.text.DecimalFormat;
public class PenniesForPay {
public static void main(String[] args) {
int numDays;
[Code] ....
totalSalary should be total number of pennies / 100....However its not picking up only day 30 of pennies which is 536,870,912 pennies and then dividing it?
View Replies
View Related
Dec 19, 2014
I have to write a program that will read a picture and then print out the number of blocks inside it.
I have to read the picture as a binary matrix of the size r - c (number of rows times number of columns). The blocks are groups of one or more adjacent elements with the value 1.
- Blocks are built exclusively of elements with value 1
- Each element with value 1 is a part of some block
- Adjacent elements with value 1 belong to the same molecule.
We only take into account the horizontal and vertical adjacency but not diagonal.
INPUT:
In the first line of the input we have the integers r and c, separated with one space.
Then we have the r lines, where each contains s 0's and 1's.
The numbers inside the individual lines are NOT separated by spaces.
The OUTPUT only print the number of blocks in the picture.
Example:
INPUT:
7 5
01000
00010
00000
10000
01000
00001
00100
OUTPUT:
6
THIS IS WHAT I CAME UP SO FAR:
import java.util.Scanner;
class Blocks{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
char ch[][];
int rowNum=sc.nextInt();
int columnNum=sc.nextInt();
[Code] ....
View Replies
View Related
Feb 13, 2014
I am working through a text and I am supposed to complete the following tasks.
Your ReadFiles.java class requires the following methods:
Method: check to see if the file exists
Method: find number of rows in csv file
Method: Converts the csv file to a mutli-dimensional array
Method: PrintArray
Method: Return array using a get method
Create a file DataAnalyzer.java. This file will be used to call the methods in ReadFiles.java. Be sure to demonstrate that all of your methods work through DataAnalyzer.java.
The problem is that it does not really provide any information on how to go about reading a file into an array. I am stuck at the third task of converting the file to an array and I have tried several ways to do this unsuccessfully. I thought that I would at least try to get things to print out (line 87) to see if I could get that to work, but all that prints in null over and over again.
Java Code:
public class DataAnalyzer {
public static void main (String[] args) {
ReadFiles aReadFiles = new ReadFiles();
aReadFiles.fileCheck();
aReadFiles.findRows();
aReadFiles.convertFile();
[Code] .....
View Replies
View Related
Jan 19, 2015
So far so good, I need to count the number of rows in a table. What I wrote:
<sql:query dataSource="${statistics}" var="result4">
SELECT COUNT(id) AS count FROM statistics;
</sql:query>
<c:forEach var="row" items="${result4.rows}">
Total players: ${result4.count}
</c:forEach>
But, it doesn't work.
View Replies
View Related
Mar 19, 2015
I am continuing on in trying to build up the basics of matrix algebra from scratch.
I have created an object class called Matrix
import java.util.ArrayList;
public class Matrix {
public int NumRows;
public int NumColumns;
// This array contains the entries of our matrix.
ArrayList<Double> entry = new ArrayList<Double>();
[Code] ......
Bottom line: a matrix has a number of rows and a number of columns, and for each pair of row and column, we have a number in our matrix. The DisplayMatrix method prints my matrix to the screen, and the GetEntry method returns a particular value. This all works fine and dandy as far as I can tell.
A fundamental operation done to matrices to obtain a special matrix called the RREF is the process of switching 2 rows. Here is a method I have written that switches two rows of a matrix, and outputs the result as a new matrix. Note that I want the original matrix to be unchanged.
// Switch two rows
public static Matrix SwapRows(Matrix A, int r1, int r2){
if(r1<1 || r1>A.NumRows || r2<1 || r2>A.NumRows)
PRINTLN("illegally switching rows");
Matrix C = A;
double dummy[] = new double[A.NumColumns];
[Code] ....
How I call this, inside a public static void main(String[] args), is as follows:
// Declares that A is a 2 by 2 matrix.
Matrix A = new Matrix(2,2);
// We now add values in. The top left entry of A is 4, the top right entry of A is 1, the bottom left entry of A is 2, and the bottom right entry of A is 6.
double pony[]= new double[4];
pony[0]=4;
pony[1]=1;
pony[2]=2;
pony[3]=6;
A.AddEntries(pony);
// We can display the matrix in the output, and it looks exactly as expected!
A.DisplayMatrix();
// I am now going to create a new matrix called B. It is going to be obtained by flipping the first and second rows of A.
//Note that I want A is stay as I initialized it.
//I dont want A to have it's 2 rows switched. I want B to the matrix obtained by switching two rows of A.
Matrix B=SwapRows(A,1,2);
B.DisplayMatrix();
// Displaying B gives me the desired result. However, if I now display A again, it is the same as B.
A.DisplayMatrix();
Why is my matrix A being modified? Of course, I am more than capable of providing more details/comments if requested. I suspect that this is a super silly mistake.
View Replies
View Related
Apr 4, 2015
I need to create a program in Java for below question
"Prompt user for three sides of triangle and determine if it is valid triangle and then compute area"
With code and Sudo Code for above question.
I am using Netbean compiler.
View Replies
View Related
Apr 22, 2014
How to find the total number of pages in java script .how to achieve it.
View Replies
View Related
Apr 24, 2015
I have to organize the rows from the lower to the higher number, the problem is, what I have only organizes the first 3 rows even If I insert 3,4,5...or 10 rows.
int arg[][] = {
{26, 39, 3, 13},
{22, 97, 17, 123},
{46, 19, 63, 123},
{1, 37, 90, 32},
{17, 37, 90, 32}};
for (int i = arg[0].length - 1; i >= 0; i--) {
for (int j = 0; j < i ; j++) {
for (int k = 0; k < i ; k++) {
if (arg[k][j] > arg[k][j + 1]) {
int temp = arg[k][j];
[Code] ....
View Replies
View Related
Aug 11, 2014
This is what I have so far, but how do I ask the user for the number or rows and give the user the option to repeat the program?
public class Pyramid {
public static void main(String[] args) {
int myLevel;
int i, j , k;
myLevel = 6;
for (i = 1; i <= myLevel; i++) {
[Code] ....
View Replies
View Related
Nov 28, 2014
So far I've got it where the program is reading the file and returning
a:0
b:0
c:0
d:0
e:0
f:0
g:0
h:0
i:0
j:0
k:0
l:0
m:0
n:0
o:0
p:0
q:0
r:0
s:0
t:0
u:0
v:0
w:0
x:0
y:0
z:0
But I need it to print the letters and then the total amount of each character that is found in the file.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
[code]....
View Replies
View Related
Dec 27, 2014
1)A factorial of a number X is equal to X*(X-1)*(X-2)*...*1.For example,3! is equal 3*2*1=6.Create a class called Factorial Algorithm which will compute and print the factorial of an integer number on the screen
2)Write a Java program to accept eight integers and a search element from the user and display whether the element is found or not.(Hint:use bubble sorting and binary search)
View Replies
View Related
Apr 19, 2014
I wanted to write a simple code that receives information from the user (double-digit number and above) and calculates it's numbers.
For instance- The user wrote a number- 234, the software will return the number 9 (2+3+4 = 9).
Tried to use a loop for that, but I got stuck since I didn't know what to write.
This is what I've wrote so far
Java Code:
import java.util.Scanner;
public class Calculate {
public static void main(String[] args) {
System.out.println("Please type a number larger than 9");
Scanner type = new Scanner(System.in);
String number1 = type.nextLine();
for( int i = 0; i < number1.length(); i++ ){
int calculate = number1.charAt(i);
}
}
} mh_sh_highlight_all('java');
View Replies
View Related
Nov 21, 2014
I wrote this code which print a black image depends on the number of rows and columns you give it
public class BlackImg {
private Mycolor[][] colorArr; //Mycolor is a class i wrote that represents colors.
// no need for showing the class here.
// so i created here an array of type Mycolor, for example:
// { {(255,255,255), {(127,127,0)} }
[Code] .....
my problem is that my output comes good except the last line ,
Output:
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
BlackImg@1db9742 //what is this line , why does it showing up ?
View Replies
View Related
Nov 9, 2014
Here is my problem: if I want to add a number of X consecutive values in an array and get the minimal total value along with the indexes that caused this result, how can I do that?
For example:
X = 2
array[4] = (5,2,8,6,7)
start adding every 2 consecutive values as following:
array[0]+array[1] = 5+2 = 7 //minimal value
array[1]+array[2] = 2+8=10
array[2]+array[3] = 8+6= 14
array[3]+array[4] = 6+7=13
output:
minimal value = 7
indexes of minimal values are: 0 and 1
View Replies
View Related
Apr 23, 2015
How would I program a counter that will return the total number of search, failed searches and correct searches. The second thing is how can a return the price of the search methods with out calling for it specifically. Here is my current code.
package stu.paston.program7;
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
InventoryClass productData= new InventoryClass();
[Code] ....
View Replies
View Related
May 22, 2014
JOptionPane.showMessageDialog(null,myRoster.totalGoalsScored + " is the total number of goals scored by all the players on the roster");
HOW CAN I CALL THE METHOD totalGoalsScored and display it using JOptionPane
ERROR: CAN NOT FIND SYMBOL
View Replies
View Related
Sep 10, 2014
Write a program that accepts the line number from the user and prints only that particular line from the Floyd triangle.
Example:
Input: 2
Output: 2 3
Input: 3
Output: 4 5 6
View Replies
View Related
Mar 16, 2014
i want create little program which enter the number, ant program says triangle exist or not. So code :
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
[code]...
So when i put first num like 1, next num like 2 and next num like 100 , program says Triangle exist.
View Replies
View Related
Jun 12, 2014
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
System.out.println("Value changed");
if(Float.parseFloat(table.getModel().getValueAt(table.getSelectedRow(), 4).toString()) != 0.0){
System.out.println(getTotal());
totalAmount.setText(""+getTotal());
}
}
});
This is what I've tried so far. What i want to do is to calculate all Total Amounts by adding all the rows (5th column only),as 5th column has total amount for 1 product, and set the text for a JTextField whenever the 5th column of any row changes its value.But this isn't working my way. I have also tried keyListener to trigger that change at Enter key typed but that also don't work for me.
View Replies
View Related
Mar 28, 2015
I have a beginning Java Program I have been working on that creates a number guessing program where the computer guesses a number that the user is thinking of within a certain range. I so far have the program below, but need getting rid of a few kinks/ adding features.
-First, I need to set it up so that the user will be prompted and give a range. It should run and produce something like this:
Welcome to this first-ever mind-guessing program!
Please pick a range (higher than 1 and no larger than 50): 32
You will choose a number between 1 and 32... and I will try to guess it.
With each of my guess, you will tell me whether I am too high (h or H), too low (l or L), match (m or M), or you want to quit (q or Q). My objective is to find the number using as few guesses as possible.
-Second, the game is supposed to give up and restart after failing the five, guesses, but for some reason, after it fails the fifth time, it prompts a fifth guess once again instead, then restarts after- I need to prevent this, so that it should look something like this:
My fourth guess is 17: h
My guess is too high?
My fifth guess is 16: h
*** I am unlucky this round. I give up.
Let's play!
My first guess is 10:
etc..
import java.util.*;
import java.lang.Math;
public class numguessprac1 {
// Declaring variables
public static String input;
public static int quit;
public static int guess;
public static int wins;
[Code] ....
View Replies
View Related
Sep 16, 2014
So I am currently writing my first assignment and have run into problems with my coding. The task was to have someone enter a 5 digit number and in return, I list each number on their respective lines. We also must create an error if a number other than 5 digits was entered. My problem is that when they enter a 1 or 2,3,4,6,7,8 digit number.. the error message occurs along with the rest of the messages (listing the numbers, etc). I want the program to end (or even re-ask to enter the numbers) if they incorrectly enter the data.
View Replies
View Related
May 5, 2014
trying to write a program that takes a user inputted number and converts it to a binary number.
Here's what I have:
package com.java2novice.algos;
import java.util.Scanner;
public class Converter {
static Scanner console = new Scanner(System.in);
public void printBinaryFormat(int number){
int binary = console.nextInt();
[Code]...
Bugs on:
Line 13
Line 17
Line 23
View Replies
View Related
Feb 27, 2015
I am trying to create a method that prints the square root of a number up to a certain number. It needs to take a single int parameter for example "n" , and then print all of the (positive) even perfect squares less than n, each on a separate line. I want the method to be called something like this:
public void Squares(int n) {
}
I need the output to look something like this:
Example: if n = 40, your code should print
4
16
36
So I have been working for a few hours now and am really stuck.
This is what I have so far:
int count = 0;
int n = 4;
int max = n;
while(count < max) {
System.out.println(n);
n = n * n;
count++;
View Replies
View Related
Dec 29, 2014
ex
Sample input:2340980
Sample Output:23498
this is the program i have tried but is dosent work
import java.util.*;
class zero
{
public static void main(String args[])
{
[Code]....
View Replies
View Related