• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Help With Some Java Concepts  RSS feed

 
Graham Wolk
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may already be familiar with me as a novice in Java. I need some help in a few things in order to further my knowledge in my java class.

Also, keep in mind that I am trying to get the hang of these problems in order to use them for practice material. I will post more problems as I move on, but for now, I need advice on some of the sections in my book.

Okay, I'll start this thread off with the second chapter of my online text book. The first chapter I am good with at this point in the course, as it had to do with introducing the compiler, JVM, lines of text, and simply arithmetic, etc.

The second chapter gets a little deeper into the gravy, and I can't say I'm fully familiar with the concepts:

Declare and initialize primitive-type variables.

Perform arithmetic on integers and cast a double to an int.

Concatenate strings and convert a string of digits to an int value.

Create and use objects of type Random and BigInteger.

Use the Java API documentation to learn about the methods of a class.

Use the Math class to perform exponentiation.

Use a Scanner object to read input from the keyboard or from a string.

Perform I/O using dialog boxes.

Implement a graphics application with lines, rectangles and ellipses of various sizes, positions and colors.



Mostly in the manipulation of strings, such as how to use substring, and creating pictures I'm still very shaky on.

Can anyone briefly summarize each of these points in a few short sentences into some sage wisdom and explain it to me better? The book does not do a very great job of explaining it (It was hand typed by my professor...)
Maybe someone could pose me a few suggestions on how to familiarize myself with them?
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mostly in the manipulation of strings, such as how to use substring

This is almost specific enough to answer. A substring is "chunk" of a string. It start with the first index and ends just before the ending index.

"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"

Now I got those examples from the documentation:

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int)

you should be able to Google "java api substring" and find it. The reason I mention this is a lot of your questions are very vague and most people won't answer them unless you've done some work yourself.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare and initialize primitive-type variables.


So what about this don't you get? What do you think it is? What have you tried?

Use this as a template for the other questions.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find details about all of these topics in Oracle's Java Tutorials and the API documentation.

Graham Wolk wrote:Declare and initialize primitive-type variables.

Primitive Data Types

Graham Wolk wrote:Perform arithmetic on integers and cast a double to an int.

Assignment, Arithmetic, and Unary Operators

Graham Wolk wrote:Concatenate strings and convert a string of digits to an int value.

Strings
Converting Between Numbers and Strings

Graham Wolk wrote:Create and use objects of type Random and BigInteger.

Beyond Basic Arithmetic
java.math.BigInteger

Graham Wolk wrote:Use the Java API documentation to learn about the methods of a class.

Java™ Platform, Standard Edition 8 API Specification

Graham Wolk wrote:Use the Math class to perform exponentiation.

Beyond Basic Arithmetic
java.lang.Math

Graham Wolk wrote:Use a Scanner object to read input from the keyboard or from a string.

Scanning

Graham Wolk wrote:Perform I/O using dialog boxes.

How to Make Dialogs

Graham Wolk wrote:Implement a graphics application with lines, rectangles and ellipses of various sizes, positions and colors.

2D Graphics
 
Graham Wolk
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:You can find details about all of these topics in Oracle's Java Tutorials and the API documentation.

Graham Wolk wrote:Declare and initialize primitive-type variables.

Primitive Data Types

Graham Wolk wrote:Perform arithmetic on integers and cast a double to an int.

Assignment, Arithmetic, and Unary Operators

Graham Wolk wrote:Concatenate strings and convert a string of digits to an int value.

Strings
Converting Between Numbers and Strings

Graham Wolk wrote:Create and use objects of type Random and BigInteger.

Beyond Basic Arithmetic
java.math.BigInteger

Graham Wolk wrote:Use the Java API documentation to learn about the methods of a class.

Java™ Platform, Standard Edition 8 API Specification

Graham Wolk wrote:Use the Math class to perform exponentiation.

Beyond Basic Arithmetic
java.lang.Math

Graham Wolk wrote:Use a Scanner object to read input from the keyboard or from a string.

Scanning

Graham Wolk wrote:Perform I/O using dialog boxes.

How to Make Dialogs

Graham Wolk wrote:Implement a graphics application with lines, rectangles and ellipses of various sizes, positions and colors.

2D Graphics



This was the exact answer I was looking for. Are these located on the API site itself? I couldn't for the life of me find anything like this on there.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Graham Wolk wrote:This was the exact answer I was looking for. Are these located on the API site itself? I couldn't for the life of me find anything like this on there.

Not really. Jesper's pointed you to the tutorials, which explain how to use common types and classes (and why you might want to). The API docs are more like a dictionary or a reference; they explain WHAT classes and interfaces do.

Don't get me wrong; the APIs are really important. And when you get a bit more practised, you'll probably be able to work out the "how" and the "why" just from reading them. But maybe not just yet...

Winston
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tutorials are an excellent resource if they are good tutorials, and Java has a lot of good tutorials. Almost all of Oracle's Java tutorials are good.

I've been writing Java for a long time but to this day, if I want to learn something about technology X in Java, my search keywords are "java X tutorial". That almost inevitably leads me to the relevant tutorial from Oracle. I highly recommend doing that.
 
Graham Wolk
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys

I am now doing a practice problem on my teacher's textbook page. The question:

"A marathon is a foot race of 26 miles and 385 yards. The best way to run a marathon is to run at the same pace for the entire race. A runner measures his or her pace in minutes and seconds per mile. Most runners in a marathon are attempting to meet a target time, expressed in hours and minutes. For example, a runner might want to complete a marathon in three hours and twenty minutes (3:20). To do this, she would need to run each mile in 7 minutes and 38 seconds (7:38).

Write a program that accepts as input the target time in the form H:MM (one digit for hours and two digits for minutes, separated by a colon) and outputs the mile pace needed in the form M:SS (one digit for minutes and two digits for seconds)."


The sample context that should be the format when the program is run is :


Enter marathon target time: 3:00
Mile pace: 6:52



Now that I have the question given, I will show you guys what I've done so far:


package marathontimes;

import java.util.Scanner;

/**
*
* @author Graham
* @version Oct 8, 2014
*/
public class MarathonTimes {

public static void main(String[] args) {

Scanner rec = new Scanner(System.in);

System.out.println("Enter marathon target time: ");

rec.hasNextBigDecimal();

*In which this space here is where I would continue, as you can see I already have the title for the mile pace time in minutes per second.


System.out.println("Mile pace: ");



}

}



I know that I need to convert the marathon total time (hours per minute) into the minutes per mile. I would have to divide the marathon time by 60 for each hour. Now the thing I'm having trouble with is how to put that into NetBeans. If there is no single int variable declared, how can I create a sort of 'universal' arithmetic operation in order to have whatever I input always be divided by the conversion factor?


Thank you in advance.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Graham Wolk wrote: . . .
Create and use objects of type Random and BigInteger.
. . .
Look here and here.

Actually one of those threads is about Big Decimal not Big Integer. Sorry if that is a mistake.


Use the Math class to perform exponentiation.
. . .
Avoid the Math class if possible for squaring. In most cases you will get faster performance and slightly less inaccuracy with x * x than with Math.pow(x, 2). Also note the types: if you multiply two primitive integers you get an integer result with the risk of overflow but Math.pow returns a double, so you never get overflow but you might get ∞ as a result. You might get underflow and get 0.0 however.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!