Question:
java program i need help with this code. i would greatly appreciate it?
jennifer j
2009-09-21 16:51:52 UTC
i have to write a gui program that converts seconds to years, weeks, days, hours, and minutes. for this problem assume 1 year is 365 days
Four answers:
?
2009-09-22 05:32:19 UTC
//pk



import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;



public class TimeConverter implements ActionListener{

JFrame f;

JPanel p;

JTextField tSecond,tYear,tWeek,tDay,tHour,tMinute;

JLabel lSecond,lYear,lWeek,lDay,lHour,lMinute;

JButton bConvert,bClear;



public TimeConverter(){

f = new JFrame ("Time Converter Ver 1.0");

f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

p = new JPanel();

p.setLayout (new GridLayout(7,2));



lSecond = new JLabel("Seconds");

lYear = new JLabel("Years");

lWeek = new JLabel("Weeks");

lDay = new JLabel("Days");

lHour = new JLabel("Hours");

lMinute = new JLabel("Minutes");



tSecond = new JTextField(10);

tYear = new JTextField(10);

tYear.setEditable(false);



tWeek = new JTextField(10);

tWeek.setEditable(false);



tDay = new JTextField(10);

tDay.setEditable(false);



tHour = new JTextField(10);

tHour.setEditable(false);



tMinute = new JTextField(10);

tMinute.setEditable(false);



bConvert = new JButton("Convert");

bConvert.addActionListener(this);

bClear = new JButton("Clear");

bClear.addActionListener (this);





p.add(lSecond);

p.add(tSecond);

p.add(lYear);

p.add(tYear);

p.add(lWeek);

p.add(tWeek);

p.add(lDay);

p.add(tDay);

p.add(lHour);

p.add(tHour);

p.add(lMinute);

p.add(tMinute);

p.add(bConvert);

p.add(bClear);



f.getContentPane ().add(p);

f.setSize(500,200);

f.setVisible(true);

}



public static void main (String[] args) {

new TimeConverter();

}



public void actionPerformed (ActionEvent e){

if(e.getSource()==bConvert){

double secconds = Double.parseDouble (tSecond.getText());

double year = secconds/(365*24*60*60);

double week = secconds/(7*24*60*60);

double day = secconds/(24*60*60);

double hour = secconds/(60*60);

double minute = secconds/(60);



tYear.setText(year+"");

tWeek.setText(week+"");

tDay.setText(day+"");

tHour.setText(hour+"");

tMinute.setText(minute+"");

}

else if(e.getSource()==bClear){

tSecond.setText("");

tYear.setText("");

tWeek.setText("");

tDay.setText("");

tHour.setText("");

tMinute.setText("");

}

}

}
KingHumpty
2009-09-21 18:21:51 UTC
write a gui program



AWT or Swing?



I'll assume Swing:



Create a JFrame class.

Create a "contentPanel" class that extends JPanel.

Add editable text components (there are a few you can choose from, but the simplest here would be JTextField). Add one for "seconds" somewhere suitable on the contentPanel.

Add readable text components to contentPanel (you have quite a few options here, but JTextField is probably the most suitable). Add one for each output value and use the inherited method from JTextComponent to set these ones read only.



Next you want to create a method for the contentPanel that takes the seconds value from the appropriate JTextField and creates the text for each of the output JTextField components and calls the appropriate method for each JTextField component to set the text within them.



Now add a JButton to the contentPanel and setup it's action listener to call the method you defined to take the value for seconds and set the other components text values.



Ok, so you should now have your JFrame (which is the main window) and a contentsPanel with suitable components and behaviour wired up. Just add the contentsPanel to the JFrame and create an instance of the JFrame and set it visible - you do those from your main method. The neatest way to do this is to put the main method within a class that extends JFrame so you have a custom JFrame class with it's own runnable main method that just causes it to build itself and show it's face - which is a reasonable way to build basic self tests in even if you later reuse components in a project with another main method defined somewhere else.
xcelled194
2009-09-21 18:25:28 UTC
convert to days. I don't know the exact syntax, but like



Function SecondConvert (seconds)



\\ since there are 3600 seconds in an hour, 24 hrs in a day,365

\\days a year, a year in seconds is 31536000

var yrs = int(seconds / 31536000 );

var wks = int((seconds - (yrs * 31536000)) / 604800);

var dys = int(seconds - int(seconds / 31536000 ) -

(seconds - (yrs * 31536000)) / 86400);

var hrs = int((seconds - (yrs * 31536000)) - int((seconds - (yrs * 31536000)) / 86400) / 3600);

var min = int(int((seconds - (yrs * 31536000) - int((seconds - (yrs * 31536000)) / 86400) / 3600)/60);



This code works by finding the integer value of a year, so if it is 1.2 years, it only takes 1. then it takes the remaining .2 years and divides months evenly into that and so on...................
anonymous
2017-01-18 10:01:43 UTC
public classification EvenOdd { private int quantity; // Constructor public EvenOdd(int n) { quantity = n; } public String posNegZero () { if (quantity > 0) return "advantageous"; if (quantity < 0) return "damaging"; return "0"; } public String evenOrOdd () { if (quantity == 0) return "neither"; if (quantity % 2 == 0) return "even"; if (quantity % 2 == a million) return "strange": } }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...